简体   繁体   English

Android HttpClient错误

[英]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. 我正在为Android开发一个应用程序,并且我尝试了大多数(如果不是全部)从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 我认为您运行的是Android4,或者至少是2.3+版本,并且您有一个操作(单击按钮),使应用程序保持忙碌状态,例如可能是联网之类的事情,而Andoid无法处理下一次单击

There it is: 那里是:

Caused by: android.os.NetworkOnMainThreadException

Do the Netwoking in background threads pls. 在后台线程中执行Netwoking。 Also you can look here for a more detailed explanations, and searching for NetworkOnMainThreadException will give nice results. 您也可以在这里查看更详细的解释,搜索NetworkOnMainThreadException将提供不错的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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