0

my app always crashes wehen I run the get function and I find no solution:

07-09 12:49:45.613: E/AndroidRuntime(1395): FATAL EXCEPTION: main
07-09 12:49:45.613: E/AndroidRuntime(1395): java.lang.IllegalStateException: Could not execute method of the activity
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.view.View$1.onClick(View.java:3724)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.view.View.performClick(View.java:4261)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.view.View$PerformClick.run(View.java:17356)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.os.Handler.handleCallback(Handler.java:615)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.os.Looper.loop(Looper.java:137)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.app.ActivityThread.main(ActivityThread.java:4921)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at dalvik.system.NativeStart.main(Native Method)
07-09 12:49:45.613: E/AndroidRuntime(1395): Caused by: java.lang.reflect.InvocationTargetException
07-09 12:49:45.613: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.view.View$1.onClick(View.java:3719)
07-09 12:49:45.613: E/AndroidRuntime(1395):     ... 11 more
07-09 12:49:45.613: E/AndroidRuntime(1395): Caused by: android.os.NetworkOnMainThreadException
07-09 12:49:45.613: E/AndroidRuntime(1395):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at de.xx.XX.postData(xx.java:315)
07-09 12:49:45.613: E/AndroidRuntime(1395):     at de.xx.XX.send_msg(xx.java:301)
07-09 12:49:45.613: E/AndroidRuntime(1395):     ... 14 more

My http get Android Java Code:

public void postData() {

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://www.xxxx.com/mobile/xxx/save.php?text=sdf"); 

        // replace with your url
        HttpResponse response;
        try {
            response = client.execute(request);

            Log.d("Response of GET request", response.toString());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } 

I hope you can help me and I apologize for my bad english. Why do we have to write as much text here, I do not know what to write.

Brot14
  • 13
  • 6

3 Answers3

1

Try : class name extends async class, the make your all http calls in , doInBackground method

above android 4.0 all https calls are allowed in async thread only

try this link for better explanation : AsyncTask Android example

Community
  • 1
  • 1
Deepak
  • 1,669
  • 1
  • 18
  • 28
0

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask

For more info see android NetworkOnMainThreadException

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

Run your code in asyntask or a different thread you can't make a network request on your main thread.

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36