android - Error in calculating distance -


i found code using json in calculating distance. tried edit use in project went wrong when click button.

here code:

public class mainactivity extends actionbaractivity { edittext e1,e2; textview t1; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     e1 = (edittext) findviewbyid (r.id.edittext1);     e2 = (edittext) findviewbyid (r.id.edittext2);     t1 = (textview) findviewbyid (r.id.textview1); } public void d (view view){     getroutdistane(0, 0, 0, 0); } public string getroutdistane(double startlat, double         startlong, double endlat, double endlong) {   string distance = "error";   string status = "error";   try {       log.e("distance link : ", "http://maps.googleapis.com/maps/api/directions/json?origin="+ startlat +e1+ startlong +"&destination="+ endlat +e2+ endlong +"&sensor=false");         jsonobject jsonobj = parser_json.getjsonfromurl("http://maps.googleapis.com/maps/api/directions/json?origin="+ startlat +e1+ startlong +"&destination="+ endlat +e2+ endlong +"&sensor=false");          status = jsonobj.getstring("status");         if(status.equalsignorecase("ok"))         {         jsonarray routes = jsonobj.getjsonarray("routes");           jsonobject 0 = routes.getjsonobject(0);          jsonarray legs = zero.getjsonarray("legs");          jsonobject zero2 = legs.getjsonobject(0);          jsonobject dist = zero2.getjsonobject("distance");          distance = dist.getstring("text");          t1.settext(distance.tostring());         }         else         {             distance = "too far";         }     } catch (jsonexception e) {         // todo auto-generated catch block         e.printstacktrace();     } return distance;   }  public static class parser_json {      public static jsonobject getjsonfromurl(string url){          //initialize         inputstream = null;         string result = "";         jsonobject jarray = null;          //http post         try{             httpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(url);             httpresponse response = httpclient.execute(httppost);             httpentity entity = response.getentity();             = entity.getcontent();          }catch(exception e){             log.e("log_tag", "error in http connection "+e.tostring());         }          //convert response string         try{             bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             result=sb.tostring();         }catch(exception e){             log.e("log_tag", "error converting result "+e.tostring());         }          //try parse string json object         try{                 jarray = new jsonobject(result);         }catch(jsonexception e){             log.e("log_tag", "error parsing data "+e.tostring());         }          return jarray;     }       public static inputstream retrievestream(string url) {              defaulthttpclient client = new defaulthttpclient();               httpget getrequest = new httpget(url);              try {                 httpresponse getresponse = client.execute(getrequest);                final int statuscode = getresponse.getstatusline().getstatuscode();                 if (statuscode != httpstatus.sc_ok) {                     return null;                }                 httpentity getresponseentity = getresponse.getentity();                return getresponseentity.getcontent();              }              catch (ioexception e) {                getrequest.abort();              }              return null;           }     }  } 

the code didn't give error in eclipse, click on button leads crash.

have added permission accessing internet application? if not crash because of this? also, doing network operation on main thread crash because of networkonmainthreadexception well. after have checked internet permissions, please use thread or asynctask run distance calculator code.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -