HttpClient.execute总是给出Exception

时间:2011-07-21 09:16:10

标签: java android httpclient

我尝试使用网页的HTML,并使用此代码执行此操作:

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);它给我例外,也在我尝试的其他代码中

1 个答案:

答案 0 :(得分: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>