我在模拟器中测试它可以正常运行。但我在手机上测试,它总是抛出异常。
public static String doGet(String url) {
try {
HttpGet method = new HttpGet(url);
DefaultHttpClient client = new DefaultHttpClient();
// HttpHost proxy = new HttpHost("10.0.0.172", 80); The two sentence added on the phone, it still can not run
// client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
method.setHeader("Connection", "Keep-Alive");
HttpResponse response = client.execute(method);
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK)
throw new Exception("");
return EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
return "Connection errors!";
}
}
我在创建项目时检查API4。并在模拟器android 1.6中测试。测试手机是在Android 2.3.3上。我也在电脑android-x86 2.2罐子里测试。那没问题。我在<uses-permission android:name="android.permission.INTERNET"/>
中添加AndroidManifest.xml
。我不知道原因。任何人的意见都很感激。
答案 0 :(得分:0)
你称这个函数为什么线程?您无法在UI线程中创建连接