2
我正在爲android開發一個應用程序,並且我嘗試了大部分(如果不是全部)從URL獲取網頁內容的方法。Android HttpClient錯誤
我所使用的代碼是:
public void login(View view) throws ClientProtocolException, IOException {
Logger logger = Logger.getLogger("schedulous");
logger.info("BUTTON CLICKED");
HttpGet request = new HttpGet("http://www.google.com/");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String entityContents = EntityUtils.toString(entity);
logger.info(entityContents);
}
而我得到的錯誤是:
09-09 17:47:02.021: E/AndroidRuntime(1611): FATAL EXCEPTION: main
09-09 17:47:02.021: E/AndroidRuntime(1611): java.lang.IllegalStateException: Could not execute method of the activity
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View$1.onClick(View.java:3591)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View.performClick(View.java:4084)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View$PerformClick.run(View.java:16966)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.Handler.handleCallback(Handler.java:615)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.Looper.loop(Looper.java:137)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 17:47:02.021: E/AndroidRuntime(1611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 17:47:02.021: E/AndroidRuntime(1611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 17:47:02.021: E/AndroidRuntime(1611): at dalvik.system.NativeStart.main(Native Method)
09-09 17:47:02.021: E/AndroidRuntime(1611): Caused by: java.lang.reflect.InvocationTargetException
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.view.View$1.onClick(View.java:3586)
09-09 17:47:02.021: E/AndroidRuntime(1611): ... 11 more
09-09 17:47:02.021: E/AndroidRuntime(1611): Caused by: android.os.NetworkOnMainThreadException
09-09 17:47:02.021: E/AndroidRuntime(1611): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-09 17:47:02.021: E/AndroidRuntime(1611): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-09 17:47:02.021: E/AndroidRuntime(1611): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-09 17:47:02.021: E/AndroidRuntime(1611): at com.schedulous.me.Main.login(Main.java:41)
09-09 17:47:02.021: E/AndroidRuntime(1611): ... 14 more
我也向你保證,我在清單中設置適當的權限。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>