我正在尝试从 url 获取 xml,但我在 HttpResponse 中有一个错误。
URL例如如下:
我的代码是:
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
谢谢