我想從服務器下載一個XML文件,我使用XMLpull解析器來處理它,但它不會每次都下載整個數據。即使我嘗試等待下載它(線程休眠)。你有什麼想法爲什麼會發生這種情況或如何解決這個問題? 這是我的功能是下載我使用線程ofcourse啓動功能的文件Android沒有得到整個數據
/*XML read*/
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
int len = 100000;
try {
URL url_get = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url_get.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
is = conn.getInputStream();
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
。
這裏是螺紋:
Thread thread = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(100);
while (while_start)
{
if(While_getadat){
try {
get_xml = downloadUrl(URL_IMEI);
Thread.sleep(2000);
Global_XML_data=red_xml(get_xml);
Thread.sleep(1000);
While_getadat=false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
UPDATE
有趣thigs,在調試模式下工作方案properlly我拿到每一塊DATAS
我真的不知道爲什麼。任何想法? – meklod400