java - Can't connect to WebService android.os.NetworkOnMainThreadException -


this question has answer here:

i trying connect php page in android app. have permissions set. however, keep getting invalid ip address. seems need asynctask. however, don't know how set up, maybe class runs separately this: how fix android.os.networkonmainthreadexception? , on link have no idea why returning rssfeed

the actual website link works. app throwing error.

arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();  namevaluepairs.add(new basicnamevaluepair("id", "123")); namevaluepairs.add(new basicnamevaluepair("name", "test name")); namevaluepairs.add(new basicnamevaluepair("family_memb_id", "456");  try {     httpclient httpclient = new defaulthttpclient();     httppost httppost = new httppost("http://mysite/insert.php");      httppost.setentity(new urlencodedformentity(namevaluepairs));     httpresponse response = httpclient.execute(httppost);     httpentity entity = response.getentity();     = entity.getcontent();  }   catch(exception e)  {      log.e("app", "failed 1: " + e.tostring());  }   try  {      bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8);      stringbuilder sb = new stringbuilder();       while ((line = reader.readline()) != null)          sb.append(line + "\n");       is.close();      result = sb.tostring();  }   catch(exception e)  {      log.e("app", "failed 2: " + e.tostring());  }   try  {      jsonobject json_data = new jsonobject(result);      code=(json_data.getint("code"));       if(code==1)      {          toast.maketext(getbasecontext(), "inserted successfully",                         toast.length_short).show();      }      else      {          toast.maketext(getbasecontext(), "sorry, try again",                         toast.length_long).show();      }  }  catch(exception e)  {      log.e("app", "failed 3: " + e.tostring());  } 

error log:

failed 1: android.os.networkonmainthreadexception
failed 2: java.lang.nullpointerexception: lock == null
failed 3: java.lang.nullpointerexception: attempt invoke virtual method 'int java.lang.string.length()' on null object reference

this not duplicate of null pointer exception; although log shows null pointer exception, problem happens before that. the null pointer side effect of real issue is.

yeah, you're on right track thinking need asynctask!

you have run network communications in thread separate main ui. it's done way because putting network requests in ui thread result in poor user experience; app hang while waiting responses servers you're contacting.

so getting separate asynctask handle network communications way go.

edit: here's indepth tutorial on how started network comms in asynctask.

another edit: network code going go asynctask. assuming you're passing in namevaluepairs code might this.

/* somewhere in ui thread */ arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();  namevaluepairs.add(new basicnamevaluepair("id", "123")); namevaluepairs.add(new basicnamevaluepair("name", "test name")); namevaluepairs.add(new basicnamevaluepair("family_memb_id", "456"); asynctask httpasynctask = new httpasynctask(); httpasynctask.execute(namevaluepairs);   /* asynctask */  public class asynctask httpasynctask extends asynctask<arraylist<namevaluepair>, void, boolean>(){     @override     protected boolean doinbackground(arraylist<namevaluepair>... params)     {          arraylist<namevaluepair> namevaluepairs = params[0];          /* http code here */     }  } 

it's worth point out using httpurlconnection recommended instead of defaulthttpclient. api easier use, it's still actively being improved , it's more efficient, in turn improves speed , saves battery.

if decide make switch httpurlconnection code in this answer shows how you'd go posting namevaluepairs.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -