Android HttpClient错误

时间:2012-09-09 22:52:39

标签: java android httpclient

我正在为Android开发一个应用程序,我尝试了大多数(如果不是全部)从URL获取网页内容的方法。

我使用的代码是:

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>

1 个答案:

答案 0 :(得分:1)

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.

我认为你至少在运行Android4或者2.3+并且你有一个动作(按钮点击),它会一直忙于应用程序,就像网络可能或其他东西而下一次点击无法由Andoid处理

它是:

Caused by: android.os.NetworkOnMainThreadException

请在后台线程中进行Netwoking。另外,您可以查看here以获取更详细的说明,搜索NetworkOnMainThreadException会得到很好的结果。