我在仿真器中測試它可以正確運行。但是我在手機上測試,總是會拋出異常。在仿真器中測試是好的,但它不能在手機上
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罐子。那沒問題。我在AndroidManifest.xml
中添加<uses-permission android:name="android.permission.INTERNET"/>
。我不知道原因。任何人的意見都很感激。
發佈您的logcat輸出 –
你檢查你的手機是否連接到互聯網? – Talha