【发布时间】:2013-04-19 10:57:06
【问题描述】:
我正在尝试从 url 获取 xml,但我在 HttpResponse 中有一个错误。
网址例如如下:
我的代码是:
public String getXML (String url){
String result = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (Exception ex) {
Toast errorToast =
Toast.makeText(getApplicationContext(),
"Error reading xml", Toast.LENGTH_LONG);
errorToast.show();
}
return result;
}
我已经在清单中设置了互联网权限。 错误在以下行:
HttpResponse httpResponse = httpClient.execute(httpPost);
并显示错误:
android.os.NetworkOnMainThreadException
谢谢
【问题讨论】:
-
你能发布错误吗?
-
试试这个:HttpPost httpPost = new HttpPost(url);