2011-12-06 34 views
0

我收到IOException從我的代碼使用AndroidHttpClient。代碼有問題嗎?無論我使用port 80還是port 443,以及使用Http Schema httphttps,都會拋出代碼。問題與AndroidHttpClient

例外情況引發client.execute ...這是一個UnknownHostException異常..我不知道爲什麼。我可以從瀏覽器中獲得服務。

 // declare locals 
    JSONObject joResponse = new JSONObject(); 
    String response = ""; 

    try 
    { 
     // prepare to execute 
     HttpHost target = new HttpHost(hostname, Const.HTTP_PORT, Const.HTTP_SCHEME); 
     HttpGet get = new HttpGet(); 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     AndroidHttpClient client = AndroidHttpClient.newInstance(null); 

     // execute 
     response = client.execute(target, get, responseHandler); // this is where the exception is thrown 

     joResponse = new JSONObject(response); 
    } 
    catch (ClientProtocolException e) 
    { 
    } 
    catch (IOException e) 
    { 
    } 
    catch(Exception e) 
    { 
    } 
+0

如果您收到一個例外則是在你的問題堆棧跟蹤顯示它沿着最佳實踐。它應該從'adb -logcat'中可用。 – Gray

+0

@格雷我已經更新了這個問題以包含例外。 e.printStackTrace引發一個'Nullpointer',因爲e = null。 – Cody

+0

'UnknownHostException'是一個'IOException'。所以你把'e.printStackTrace()'放在那裏,'e'是空的?從JVM的角度來看這是不可能的。如果它被捕獲,那麼它不是空的。 – Gray

回答

1

您是否在應用程序中聲明瞭互聯網許可?

你需要下面的行添加到您的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 
+0

是的,我擁有它。 – Cody