Mike
Mike

Reputation: 1261

error on httppost with Android

I have look through most of the q/a here at Stackoverflow and through the internet for the answer before I decided to reach out here..

I'm trying call a php page on my website and I keep getting this error when the httpclient.execute is called:

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)

So I read through some post and one thing I made sure that I had the

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

added to my manifest (which I do) below is a snip-it I got from the internet that I call from a button onclick event:

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

full log:

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

Upvotes: 4

Views: 366

Answers (2)

SteveR
SteveR

Reputation: 2506

It's a NetworkOnMainThreadException ... You are trying to perform a networking operation on main thread, it's not allowed in Main UI Thread of Android Application.

To solve this either use AsyncTask or Thread and put your Network Related Operation in it.

Upvotes: 1

Andrew Wilkinson
Andrew Wilkinson

Reputation: 248

Looks like you're getting a NetworkOnMainThreadException

Try running it on a different thread?

From the Javadocs:

"The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness. "

Upvotes: 2

Related Questions