2012-11-21 48 views
0

我在仿真器中測試它可以正確運行。但是我在手機上測試,總是會拋出異常。在仿真器中測試是好的,但它不能在手機上

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"/>。我不知道原因。任何人的意見都很感激。

+7

發佈您的logcat輸出 –

+1

你檢查你的手機是否連接到互聯網? – Talha

回答

0

你說這個函數是什麼線程?你不能創建連接在UI線程

+0

Android中的2.2+版本需要調用線程或異步任務的網絡連接,然後解決問題,我猜如TrầnSĩLong提到的 – iabhi

+1

它適用於Android 2.3+ –

+0

感謝您的指正: ) – iabhi

相關問題