2011-07-21 53 views
1

我試圖把一個網頁的HTML和我使用此代碼這樣做:HttpClient.execute總是給人異常

HttpClient httpClient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    HttpGet httpGet = new HttpGet("http://www.google.com"); 
    HttpResponse response; 
    try { 
     response = httpClient.execute(httpGet, localContext); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

,並始終在response = httpClient.execute(httpGet, localContext);它給我的異常,也是我試過其他代碼

+0

哪個異常,哪些是輸出(堆棧跟蹤,錯誤消息)? – hage

+0

您可以發佈您在LogCat中看到的錯誤消息嗎? – TofferJ

+0

java.net.UnknownHostException:www.google.com – MTA

回答

6

您是否嘗試過在清單文件中設置INTERNET權限?

示例代碼:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="sample.hello" 
    android:versionCode="1" 
    android:versionName="1.0"> 

<!--- Here is where you put your permissions. ---> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".HelloWorld" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 
</manifest> 
+0

我在這個android的東西新,所以我可以添加它? – MTA

+0

請參閱http://developer.android.com/guide/topics/manifest/manifest-intro.html。 – Wroclai

+0

@MTA:看到我編輯的答案。 – Wroclai