【发布时间】:2013-04-06 14:13:59
【问题描述】:
我使用 android 版本 2 运行我的应用程序,它工作正常,但是当我使用版本 4 运行它时,它在request.setURI(new URI(url)); 行中出现异常
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
try {
request.setURI(new URI(url));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("http\n");
}
HttpResponse httpResponse = client.execute(request);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
我不明白是什么问题。
【问题讨论】:
-
请发布异常的堆栈跟踪
-
你是在异步任务中做这项工作吗?因为从 andorid 4 开始,你不能在主线程上建立网络连接。 AsyncTask tutorial
-
没有。我不使用异步任务
-
slayton try catch 没有出现异常,所以我找不到问题所在
-
logcat 中应该有什么。发布您的日志猫。并检查您的 URI 是否正常。而且,如果可以从您正在测试的设备访问该链接,还请查看您是否拥有互联网权限。