我有一个问题,我想解析网络上的 XML 文件。现在我的 HttpResponce 读取该 XML 文件的时间太多了,所以我的 GUI 出现“成为运行时错误”错误。
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://chukk.nuzoka.com/name.xml");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
感谢任何帮助:)
最佳答案
作为 UI 编程的一般规则,不仅是 Android,您应该将任何繁重的处理/耗时任务转移到后台线程。
有很好的工具可以帮助您在后台执行长任务并将其最终/中间结果发布到 Android 中的 UI 线程,例如 AsyncTask和 Handlers .
关于android - Android 中的 HttpResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9457456/