-1

I m trying to delete a row having id 109 from mySQL database on a web server using my android device (through web services). Web service is working fine with specific url and id when I check on browser. But when I execute my android code, i got following exception in Logcat

android.os.NetworkOnMainThreadException

by trying this following code

try{
    String url = "http://example.com/user/delete";
    String u_id = 109;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("id",u_id));
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost); //problem is in this line but I don't know what the problem actually is
    HttpEntity entity = response.getEntity();
    Log.e("pass 1", "connection success ");
}
catch(Exception e){
    Log.e("Fail 1", e.toString());
}

I didn't understand what this exception means and how to solve this problem

Great thanks for your valuable time and help in advance

MK Bukhari
  • 75
  • 1
  • 9
  • try using asynTask in android – KOTIOS Mar 05 '15 at 10:45
  • pls post the full stacktrace of the exception – kamokaze Mar 05 '15 at 10:46
  • i tried but same exception appears – MK Bukhari Mar 05 '15 at 10:46
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – 2Dee Mar 05 '15 at 10:48
  • @kamokae this is the only exception i got button click – MK Bukhari Mar 05 '15 at 10:48
  • @2Dee i didn't get what you r trying to say – MK Bukhari Mar 05 '15 at 10:49
  • It means this question has been asked before and a simple Google search would have provided you with more than enough information on this particular exception... – 2Dee Mar 05 '15 at 10:50
  • @2Dee i m trying to search google and many different platforms from 2 days. i didn't get any help after that i came here – MK Bukhari Mar 05 '15 at 10:53
  • I'm sorry, I cannot believe that, when I search for "NetworkOnMainThreadException", I get 84300 results, including the duplicate I pointed out as first result... – 2Dee Mar 05 '15 at 10:56
  • there are results about NetworkOnMainThreadException but i m asking 4 specific solution not the 84300 – MK Bukhari Mar 05 '15 at 10:58
  • the main solution was use of asynktask which i tried but nothing happens so i came here to experts. but i think these experts have no solution for my specific question – MK Bukhari Mar 05 '15 at 11:01
  • I think you fail to understand that just because *you* could not make it work doesn't mean that the answer upvoted more than 1000 times is not correct, the issue is most probably that your AsyncTask code was wrong. On top of this, there are other proposed solutions, have you tried those ? Finally, the code you posted doesn't show it is used in an AsyncTask or not, the question doesn't mention what you have tried from the other similar questions, so there is not enough information for others to help... – 2Dee Mar 05 '15 at 11:13

1 Answers1

0

How to fix android.os.NetworkOnMainThreadException?

Here you go. You cannot perform network operations on the main UI thread, you need to either use asynctask or create your own thread.

Community
  • 1
  • 1
  • dear i also try asynctask but the same exception appearing – MK Bukhari Mar 05 '15 at 10:51
  • can anyone give me the specific solution...? – MK Bukhari Mar 05 '15 at 10:56
  • Well, the solution is for you to use asynctask... If asynctask is not working then its another problem, and if its another problem we need to see the full asynctask code to help you. –  Mar 05 '15 at 10:57