我在模拟器中测试它可以正常运行。但是我在手机上测试,总是抛出异常。
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 jar 中进行了测试。那没问题。我加<uses-permission android:name="android.permission.INTERNET"/>
进去AndroidManifest.xml
。我不知道原因。任何人的意见都是感谢的。