android - Getting latitude and longitude from address does not work (Error at InputStream) -


i trying latitude , longitude of place following code:

public latlng findaddress(string place) {         try {             httpurlconnection conn = null;         stringbuilder jsonresults = new stringbuilder();         string googlemapurl = "http://maps.googleapis.com/maps/api/geocode/json?address="                 + place + "&sensor=false";          url url = new url(googlemapurl);         conn = (httpurlconnection) url.openconnection();         inputstreamreader in = new inputstreamreader(conn.getinputstream());         int read;         char[] buff = new char[1024];         while ((read = in.read(buff)) != -1) {             jsonresults.append(buff, 0, read);         }         string = "";         stringbuilder sb = jsonresults;             jsonobject jsonobj = new jsonobject(sb.tostring());             jsonarray resultjsonarray = jsonobj.getjsonarray("results");             jsonobject before_geometry_jsonobj = resultjsonarray                     .getjsonobject(0);             jsonobject geometry_jsonobj = before_geometry_jsonobj                     .getjsonobject("geometry");             jsonobject location_jsonobj = geometry_jsonobj                     .getjsonobject("location");             string lat_helper = location_jsonobj.getstring("lat");             double lat = double.valueof(lat_helper);             string lng_helper = location_jsonobj.getstring("lng");             double lng = double.valueof(lng_helper);             return new latlng(lat, lng);         } catch (exception e) {              // todo auto-generated catch block             e.printstacktrace();             return new latlng(0,0);         }      } 

but when run it, goes catch-part (right after

 conn = (httpurlconnection) url.openconnection();  inputstreamreader in = new inputstreamreader(conn.getinputstream()); 

part.) after time revising code, not seem have found reason behind phenomenon. (the reason why not use geocoder service because not stable, , requires reboot on every device have tested on)

in meanwhile have solved it. got android.os.networkonmainthreadexception, should not have run network call on main thread. thank help.


Comments