在android上请求发帖的问题

时间:2014-03-08 06:29:28

标签: java android http

当我尝试在我的Android应用程序上发布帖子请求时系统会给我这个错误:

03-07 17:57:20.657: E/AndroidRuntime(2576): FATAL EXCEPTION: main
03-07 17:57:20.657: E/AndroidRuntime(2576): Process: sup.supreme, PID: 2576
03-07 17:57:20.657: E/AndroidRuntime(2576): java.lang.IllegalStateException: Could not execute method of the activity
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.view.View$1.onClick(View.java:3823)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.view.View.performClick(View.java:4438)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.view.View$PerformClick.run(View.java:18422)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.os.Handler.handleCallback(Handler.java:733)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.os.Looper.loop(Looper.java:136)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at java.lang.reflect.Method.invoke(Method.java:515)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at dalvik.system.NativeStart.main(Native Method)
03-07 17:57:20.657: E/AndroidRuntime(2576): Caused by: java.lang.reflect.InvocationTargetException
03-07 17:57:20.657: E/AndroidRuntime(2576):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at java.lang.reflect.Method.invoke(Method.java:515)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.view.View$1.onClick(View.java:3818)
03-07 17:57:20.657: E/AndroidRuntime(2576):     ... 11 more
03-07 17:57:20.657: E/AndroidRuntime(2576): Caused by: android.os.NetworkOnMainThreadException
03-07 17:57:20.657: E/AndroidRuntime(2576):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-07 17:57:20.657: E/AndroidRuntime(2576):     at sup.supreme.MainActivity.find(MainActivity.java:88)

这是我用来发出请求的代码:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(new Connection().Url()+":"+new Connection().Port()+"/Punche/ByName");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("name", "n"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您无法在主线程上执行网络请求。您必须在AsyncTask或Thread上执行此操作。

答案 1 :(得分:1)

看起来你正在你的ui线程上运行网络代码。确保所有代码都运行在与ui线程不同的线程上。查看here以获取实现此目的的正确方法的示例。