Reputation: 693
when i try to make a post request on my android app the system give me this error:
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)
This is the code that i use to make the request:
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
}
Thanks for the help.
Upvotes: 0
Views: 36
Reputation: 471
It looks like you're running network code on your ui thread. Make sure that all your code runs on a different thread than the ui thread. Look here for an example on the correct way to implement this.
Upvotes: 1
Reputation: 93728
You can't do network requests on the main thread. You must do it on an AsyncTask or Thread.
Upvotes: 1