简体   繁体   中英

Android HttpClient Error

I am developing an app for android and I have tried most, if not all, methods of getting webpage content from a URL.

The code which I have used is:

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);
}

And the error that I get is:

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

I also assure you that I have the proper permissions set in the manifest.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
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.

I think you are running Android4, or 2.3+ at least and you have an action ( button click) wich keeps busy the app, like a networking maybe or something and the next click can't be handled by Andoid

There it is:

Caused by: android.os.NetworkOnMainThreadException

Do the Netwoking in background threads pls. Also you can look here for a more detailed explanations, and searching for NetworkOnMainThreadException will give nice results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM