java - Exception while downloading URL in google maps -
part of application has map should display place in map when entered in auto filled text view.
i able handle exception map not displaying correct place, log cat shows message saying "exception while downloading url".i not sure problem is.
mainactivity
public class autocomplete extends fragmentactivity { autocompletetextview atvplaces; downloadtask placesdownloadtask; downloadtask placedetailsdownloadtask; parsertask placesparsertask; parsertask placedetailsparsertask; googlemap googlemap; final int places=0; final int places_details=1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_autocomplete); // getting reference autocompletetextview atvplaces = (autocompletetextview) findviewbyid(r.id.atv_places); atvplaces.setthreshold(1); // adding textchange listener atvplaces.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { // creating downloadtask download google places matching "s" placesdownloadtask = new downloadtask(places); // getting url google places autocomplete api string url = getautocompleteurl(s.tostring()); // start downloading google places // causes execute doinbackground() of downloadtask class placesdownloadtask.execute(url); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } }); // setting item click listener autocompletetextview dropdown list atvplaces.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int index, long id) { listview lv = (listview) arg0; simpleadapter adapter = (simpleadapter) arg0.getadapter(); hashmap<string, string> hm = (hashmap<string, string>) adapter.getitem(index); // creating downloadtask download places details of selected place placedetailsdownloadtask = new downloadtask(places_details); // getting url google places details api string url = getplacedetailsurl(hm.get("reference")); // start downloading google place details // causes execute doinbackground() of downloadtask class placedetailsdownloadtask.execute(url); } }); } private string getautocompleteurl(string place){ // obtain browser key https://code.google.com/apis/console string key = "key=aizasycfdxatlz7jtm6mevy9xh_3_g_ivc5ysxe"; // place be searched string input = "input="+place; // place type searched string types = "types=geocode"; // sensor enabled string sensor = "sensor=false"; // building parameters web service string parameters = input+"&"+types+"&"+sensor+"&"+key; // output format string output = "json"; // building url web service string url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters; return url; } private string getplacedetailsurl(string ref){ // obtain browser key https://code.google.com/apis/console string key = "key=your_api_key"; // reference of place string reference = "reference="+ref; // sensor enabled string sensor = "sensor=false"; // building parameters web service string parameters = reference+"&"+sensor+"&"+key; // output format string output = "json"; // building url web service string url = "https://maps.googleapis.com/maps/api/place/details/"+output+"?"+parameters; return url; } /** method download json data url */ private string downloadurl(string strurl) throws ioexception { string data = ""; inputstream istream = null; httpurlconnection urlconnection = null; try{ url url = new url(strurl); // creating http connection communicate url urlconnection = (httpurlconnection) url.openconnection(); // connecting url urlconnection.connect(); // reading data url istream = urlconnection.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(istream)); stringbuffer sb = new stringbuffer(); string line = ""; while( ( line = br.readline()) != null){ sb.append(line); } data = sb.tostring(); br.close(); }catch(exception e){ log.d("exception while downloading url", e.tostring()); }finally{ istream.close(); urlconnection.disconnect(); } return data; } // fetches data url passed private class downloadtask extends asynctask<string, void, string>{ private int downloadtype=0; // constructor public downloadtask(int type){ this.downloadtype = type; } @override protected string doinbackground(string... url) { // storing data web service string data = ""; try{ // fetching data web service data = downloadurl(url[0]); }catch(exception e){ log.d("background task",e.tostring()); } return data; } @override protected void onpostexecute(string result) { super.onpostexecute(result); switch(downloadtype){ case places: // creating parsertask parsing google places placesparsertask = new parsertask(places); // start parsing google places json data // causes execute doinbackground() of parsertask class placesparsertask.execute(result); break; case places_details : // creating parsertask parsing google places placedetailsparsertask = new parsertask(places_details); // starting parsing json string // causes execute doinbackground() of parsertask class placedetailsparsertask.execute(result); } } } /** class parse google places in json format */ private class parsertask extends asynctask<string, integer, list<hashmap<string,string>>> { int parsertype = 0; public parsertask(int type){ this.parsertype = type; } @override protected list<hashmap<string, string>> doinbackground(string... jsondata) { jsonobject jobject; list<hashmap<string, string>> list = null; try{ jobject = new jsonobject(jsondata[0]); switch(parsertype){ case places : placejsonparser placejsonparser = new placejsonparser(); // getting parsed data list construct list = placejsonparser.parse(jobject); break; case places_details : placedetailsjsonparser placedetailsjsonparser = new placedetailsjsonparser(); // getting parsed data list construct list = placedetailsjsonparser.parse(jobject); } }catch(exception e){ log.d("exception", e.tostring()); } return list; } @override protected void onpostexecute(list<hashmap<string, string>> result) { switch(parsertype){ case places : string[] = new string[] { "description"}; int[] = new int[] { android.r.id.text1 }; // creating simpleadapter autocompletetextview simpleadapter adapter = new simpleadapter(getbasecontext(), result, android.r.layout.simple_list_item_1, from, to); // setting adapter atvplaces.setadapter(adapter); break; case places_details : hashmap<string, string> hm = result.get(0); // getting latitude parsed data double latitude = double.parsedouble(hm.get("lat")); // getting longitude parsed data double longitude = double.parsedouble(hm.get("lng")); // getting reference supportmapfragment of activity_main.xml supportmapfragment fm = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map); // getting googlemap supportmapfragment googlemap = fm.getmap(); latlng point = new latlng(latitude, longitude); cameraupdate cameraposition = cameraupdatefactory.newlatlng(point); cameraupdate camerazoom = cameraupdatefactory.zoomby(5); // showing user input location in google map googlemap.movecamera(cameraposition); googlemap.animatecamera(camerazoom); markeroptions options = new markeroptions(); options.position(point); options.title("position"); options.snippet("latitude:"+latitude+",longitude:"+longitude); // adding marker in google map googlemap.addmarker(options); break; } } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } }
parsedetailsjsonparser.java
package zybo.example.ramz.map_webview; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; import java.util.list; public class placedetailsjsonparser { /** receives jsonobject , returns list */ public list<hashmap<string,string>> parse(jsonobject jobject){ double lat = double.valueof(0); double lng = double.valueof(0); hashmap<string, string> hm = new hashmap<string, string>(); list<hashmap<string, string>> list = new arraylist<hashmap<string,string>>(); try { lat = (double)jobject.getjsonobject("result").getjsonobject("geometry").getjsonobject("location").get("lat"); lng = (double)jobject.getjsonobject("result").getjsonobject("geometry").getjsonobject("location").get("lng"); } catch (jsonexception e) { e.printstacktrace(); }catch(exception e){ e.printstacktrace(); } hm.put("lat", double.tostring(lat)); hm.put("lng", double.tostring(lng)); list.add(hm); return list; } }
logcat
reconstruct branch: au_linux_android_la.bf.1.1.1_rb1.05.00.02.042.016 + 62ca4eb + acd831d + 9f8b442 + e027a02 + cba30ba + 53c303a + a649d79 + 23e16f8 + 5e97da7 + cbd2a44 + 33d072a + 7aacf06 + 72b33e7 + 28f6f60 + b4c13d8 + nothing 09-30 14:44:58.321 2965-3120/zybo.example.ramz.map_webview i/openglrenderer﹕ initialized egl, version 1.4 09-30 14:44:58.390 2965-3120/zybo.example.ramz.map_webview d/openglrenderer﹕ enabling debug mode 0 09-30 14:44:58.485 2965-3120/zybo.example.ramz.map_webview v/renderscript﹕ application requested cpu execution 09-30 14:44:58.498 2965-3120/zybo.example.ramz.map_webview v/renderscript﹕ 0xb82914f0 launching thread(s), cpus 4 09-30 14:45:05.291 2965-3043/zybo.example.ramz.map_webview d/volley﹕ [8830] a.a: http response request=<[ ] https://clients4.google.com/glm/mmap/api 0x99e6744e normal 1> [lifetime=7134], [size=60], [rc=200], [retrycount=0] 09-30 14:45:05.298 2965-2965/zybo.example.ramz.map_webview d/volley﹕ [1] p.b: 7147 ms: [ ] https://clients4.google.com/glm/mmap/api 0x99e6744e normal 1 09-30 14:45:05.712 2965-3044/zybo.example.ramz.map_webview d/volley﹕ [8831] a.a: http response request=<[ ] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.1581,on_create.1,on_resume.1,init.378&irt=1581,379,399,378 0x4d844933 normal 2> [lifetime=6368], [size=0], [rc=204], [retrycount=0] 09-30 14:45:05.759 2965-2965/zybo.example.ramz.map_webview d/volley﹕ [1] p.b: 6416 ms: [ ] https://csi.gstatic.com/csi?s=maps_android_api&v=3&action=map_start_up&it=map_load.1581,on_create.1,on_resume.1,init.378&irt=1581,379,399,378 0x4d844933 normal 2 09-30 14:45:14.452 2965-3124/zybo.example.ramz.map_webview d/exception while downloading url﹕ java.io.filenotfoundexception: https://maps.googleapis.com/maps/api/place/autocomplete/json?input=delhi, india&types=geocode&sensor=false&key=aizasycfdxatlz7jtm6mevy9xh_3_g_ivc5ysxe 09-30 14:45:14.459 2965-3124/zybo.example.ramz.map_webview d/background task﹕ java.lang.nullpointerexception: attempt invoke virtual method 'void java.io.inputstream.close()' on null object reference 09-30 14:45:17.503 2965-3124/zybo.example.ramz.map_webview d/exception﹕ org.json.jsonexception: end of input @ character 0 of 09-30 14:45:17.516 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ org.json.jsonexception: no value result 09-30 14:45:17.519 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ org.json.jsonobject.get(jsonobject.java:389) 09-30 14:45:17.519 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ org.json.jsonobject.getjsonobject(jsonobject.java:609) 09-30 14:45:17.520 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ zybo.example.ramz.map_webview.placedetailsjsonparser.parse(placedetailsjsonparser.java:25) 09-30 14:45:17.520 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ zybo.example.ramz.map_webview.autocomplete$parsertask.doinbackground(autocomplete.java:280) 09-30 14:45:17.520 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ zybo.example.ramz.map_webview.autocomplete$parsertask.doinbackground(autocomplete.java:254) 09-30 14:45:17.520 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ android.os.asynctask$2.call(asynctask.java:292) 09-30 14:45:17.521 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ java.util.concurrent.futuretask.run(futuretask.java:237) 09-30 14:45:17.521 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 09-30 14:45:17.521 2965-3124/zybo.example.ramz.map_webview w/system.err﹕ @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)
Comments
Post a Comment