使用Android的httppost出错

时间:2012-08-19 16:58:33

标签: php android http-post

在我决定到达这里之前,我已经在Stackoverflow和互联网上查看了大部分q / a的答案。

我正在尝试在我的网站上调用一个php页面,并且在调用httpclient.execute时我一直收到此错误:

08-19 12:42:33.889: E/AndroidRuntime(5340): FATAL EXCEPTION: main 08-19 12:42:33.889:
E/AndroidRuntime(5340): java.lang.IllegalStateException: Could not execute method of the activity 08-19 12:42:33.889: E/AndroidRuntime(5340):
at android.view.View$1.onClick(View.java:3046)

所以我读了一些帖子和一件事我确定我有

<uses-permission android:name="android.permission.INTERNET" />

添加到我的清单(我这样做)下面是我从互联网上获得的一个剪辑 - 我从按钮onclick事件调用:

 HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://mywebsite/scripts/test.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
            nameValuePairs.add(new BasicNameValuePair("function", "1"));
            nameValuePairs.add(new BasicNameValuePair("user", "username"));
            nameValuePairs.add(new BasicNameValuePair("pass", "123"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            Log.e("log_tag", response.toString());


        } catch (ClientProtocolException e) {
             Log.e("log_tag", e.toString());
        } catch (IOException e) {
            Log.e("log_tag", e.toString());
        }

完整日志:

08-19 12:42:16.365: I/System.out(5340): Sending WAIT chunk
08-19 12:42:16.365: W/ActivityThread(5340): Application com.example.equinetravels is waiting for the debugger on port 8100...
08-19 12:42:16.373: I/dalvikvm(5340): Debugger is active
08-19 12:42:16.373: I/System.out(5340): Debugger has connected
08-19 12:42:16.373: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:16.576: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:16.779: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:16.982: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.186: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.381: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.584: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.779: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.982: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.186: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.381: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.584: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.787: I/System.out(5340): debugger has settled (1432)
08-19 12:42:19.037: D/libEGL(5340): loaded /system/lib/egl/libGLES_android.so
08-19 12:42:19.045: D/libEGL(5340): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-19 12:42:19.053: D/libEGL(5340): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-19 12:42:19.053: D/libEGL(5340): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-19 12:42:19.162: D/OpenGLRenderer(5340): Enabling debug mode 0
08-19 12:42:33.834: D/AndroidRuntime(5340): Shutting down VM
08-19 12:42:33.834: W/dalvikvm(5340): threadid=1: thread exiting with uncaught exception (group=0x40ab6210)
08-19 12:42:33.889: E/AndroidRuntime(5340): FATAL EXCEPTION: main
08-19 12:42:33.889: E/AndroidRuntime(5340): java.lang.IllegalStateException: Could not execute method of the activity
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.view.View$1.onClick(View.java:3046)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.view.View.performClick(View.java:3526)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.view.View$PerformClick.run(View.java:14133)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.os.Handler.handleCallback(Handler.java:605)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.os.Looper.loop(Looper.java:137)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.app.ActivityThread.main(ActivityThread.java:4697)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at dalvik.system.NativeStart.main(Native Method)
08-19 12:42:33.889: E/AndroidRuntime(5340): Caused by: java.lang.reflect.InvocationTargetException
08-19 12:42:33.889: E/AndroidRuntime(5340):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.view.View$1.onClick(View.java:3041)
08-19 12:42:33.889: E/AndroidRuntime(5340):     ... 11 more
08-19 12:42:33.889: E/AndroidRuntime(5340): Caused by: android.os.NetworkOnMainThreadException
08-19 12:42:33.889: E/AndroidRuntime(5340):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1119)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at java.net.InetAddress.lookupHostByName(InetAddress.java:441)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at com.example.equinetravels.MainActivity.postData(MainActivity.java:79)
08-19 12:42:33.889: E/AndroidRuntime(5340):     at com.example.equinetravels.MainActivity.sendLogon(MainActivity.java:47)
08-19 12:42:33.889: E/AndroidRuntime(5340):     ... 14 more

2 个答案:

答案 0 :(得分:2)

看起来你正在收到NetworkOnMainThreadException

尝试在其他线程上运行它?

来自Javadocs:

“应用程序尝试在其主线程上执行网络操作时引发的异常。

仅针对Honeycomb SDK或更高版本的应用程序进行此操作。针对早期SDK版本的应用程序可以在其主要事件循环线程上进行网络连接,但是非常不鼓励这样做。请参阅文档设计响应性。 “

答案 1 :(得分:1)

这是一个NetworkOnMainThreadException ...您正在尝试在主线程上执行网络操作,在Android应用程序的主UI线程中不允许这样做。

要解决此问题,请使用AsyncTaskThread并将网络相关操作放入其中。