android.os.NetworkOnMainThreadException while Parsing data from JSON -


hi friends developing application requires:

  • put country name in list.
  • country list json.

my webservice is:

https://maps.googleapis.com/maps/api/geocode/json

in getting following error in convert countryname langitude , latitude.

the error is:

maps call url::android.os.networkonmainthreadexception tmp album countryall::anguilla error in parsig json:org.json.jsonexception: index 4 out of range [0..1) error in post execute:java.lang.nullpointerexception 

my code json is:

 public class getlatlngasynctask extends asynctask<arraylist<string>, void, string>     {         jsonobject jobject;         jsonobject places = null;         string lat;         progressdialog progressdialog;         protected void onpreexecute()          {             super.onpreexecute();                         progressdialog = new progressdialog(map_view.this);             progressdialog.settitle("getting users location");             progressdialog.setmessage("please wait");             progressdialog.setcancelable(false);             progressdialog.setindeterminate(true);             progressdialog.show();         }          @override         protected string doinbackground(arraylist<string>... params)          {             try             {                  double lng1 = 0;                  double lat1 = 0;                  lng1=lat1=0;                   stringbuilder stringbuilder = new stringbuilder();                   jsonobject jsonobject = new jsonobject();                   try                  {                      for(int i=0;i<maplistdata.size();i++)                      {                          string countryall=maplistdata.get(i).get("country");                           countryall = countryall.replaceall(" ","%20");                           httppost httppost = new httppost("http://maps.google.com/maps/api/geocode/json?address=" + countryall + "&sensor=false");                           httpclient client = new defaulthttpclient();                           httpresponse response;                          stringbuilder = new stringbuilder();                           response = client.execute(httppost);                          httpentity entity = response.getentity();                          inputstream stream = entity.getcontent();                          int b;                           while ((b = stream.read()) != -1)                           {                              stringbuilder.append((char) b);                          }                           try                           {                              jsonobject = new jsonobject(stringbuilder.tostring());                               log.v("tmp album","tmp album countryall::"+countryall);                                lng1 = ((jsonarray)jsonobject.get("results")).getjsonobject(i)                                          .getjsonobject("geometry").getjsonobject("location")                                          .getdouble("lng");                              lat1 = ((jsonarray)jsonobject.get("results")).getjsonobject(i)                                          .getjsonobject("geometry").getjsonobject("location")                                          .getdouble("lat");                               log.v("langitude is:",""+lng1);                              log.v("latitude is:",""+lat1);                               tmp_album1 = new hashmap<string, string>();                              tmp_album1.put("lng",""+lng1);                              tmp_album1.put("lat",""+lat1);                              maplistdata1.add(tmp_album1);                               log.v("map list","map list1::"+maplistdata1);                            }                          catch (jsonexception e)                           {                              log.v("error","error in parsig json:"+e);                          }                      }                  }                  catch (exception e)                   {                      log.v("error in maps","maps call url::"+e);                  }              }                catch(exception e)             {                 log.v("error in maps","maps asyc::"+e);             }             return lat;         }          @override         protected void onpostexecute(string result)          {             // todo auto-generated method stub             super.onpostexecute(result);             try             {                 for(int i=0;i<maplistdata1.size();i++)                 {                     double lang=double.valueof(maplistdata1.get(i).get("lng"));                     double lat=double.valueof(maplistdata1.get(i).get("lat"));                      markeroptions marker = new markeroptions()                         .position(new latlng(lang,lat))                         .title("your friend in "+country);                      marker.icon(bitmapdescriptorfactory                             .defaultmarker(bitmapdescriptorfactory.hue_red));                      googlemap.addmarker(marker);                     log.v("marker","marker added");                      cameraposition cameraposition = new cameraposition.builder()                         .target(new latlng(lang,lat))                         .zoom(2).build();                     googlemap.animatecamera(cameraupdatefactory.newcameraposition(cameraposition));                 }             }             catch(exception e)             {                 log.v("error","error in post execute:"+e);             }             progressdialog.dismiss();         } 

please me. tell me doing mistake.

thanks alot in advance.

enable strict mode remove exception. put code after super.oncreate() of activity this:-

super.oncreate(savedinstancestate); **strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy);**  setcontentview(r.layout.main); 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -