spring - Java : Google Reverse Geocoding with jackson API -
i want address using google maps api. in project using jackson parser.
i want know that, how can expected result i.e. :-
"a10, dhamidhar rd, yashkamal society, vasna, ahmedabad, gujarat 380007, india"
i want ignore other fields in api. there many objects in json file. want fetch :
" "formatted_address" : "a10, dhamidhar rd, yashkamal society, vasna, ahmedabad, gujarat 380007, india","
http://maps.google.com/maps/api/geocode/json?latlng=23.0043673,72.5411868999996&sensor=false
thank you
this have tried
package com.example.api.batch; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.list; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import com.fasterxml.jackson.core.jsonparseexception; import com.fasterxml.jackson.core.jsonprocessingexception; import com.fasterxml.jackson.databind.jsonnode; import com.fasterxml.jackson.databind.objectmapper; import com.fasterxml.jackson.databind.node.objectnode; import com.fasterxml.jackson.databind.util.jsonpobject; public class geocodeaddressparser { @suppresswarnings("deprecation") public void getlocationinfo( string lat, string lng) throws jsonprocessingexception, ioexception { httpget httpget = new httpget("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=false"); @suppresswarnings("resource") httpclient client = new defaulthttpclient(); httpresponse response; stringbuilder stringbuilder = new stringbuilder(); try { response = client.execute(httpget); httpentity entity = response.getentity(); inputstream stream = entity.getcontent(); int b; while ((b = stream.read()) != -1) { stringbuilder.append((char) b); } } catch (clientprotocolexception e) { } catch (ioexception e) { } objectmapper mapper = new objectmapper(); jsonnode array = mapper.readvalue(stringbuilder.tostring(), jsonnode.class); jsonnode object = array.get(0); string reportkey = object.get("results").textvalue(); // logger.info("exportcontroller : generatesexportexcel : parameters: {}", reportkey); // system.out.println("map keys:\n"+rawdata.getstatus()); // // list<geocodegetresult> locations = rawdata.getresults(); // // // for(int i=0; < locations.size(); i++){ // geocodegetresult object = locations.get(i); // system.out.println(object); // } // for(int i=0; < locations.size(); i++){ // sourcelocation object = locations.get(i); // //system.out.println(object.getclass().getname()+" "+object); // sourcelocation converted = convertsourcelocation(object); // system.out.println(converted); // tobeinserted.add(converted); // } // jsonpobject jsonobject = new jsonpobject(); // try { // // objectnode node = mapper.createobjectnode(); // jsonnode actualobj = mapper.readtree(stringbuilder.tostring()); // // jsonobject = new jsonobject(); // } catch (jsonparseexception e) { // e.printstacktrace(); // } // return mapper; } public static void main(string[] args) throws jsonprocessingexception, ioexception{ geocodeaddressparser ref = new geocodeaddressparser(); ref.getlocationinfo("23.0043673","72.5411868999996"); // objectmapper location; // string location_string; // try { // //get json array called "results" , 0th complete object json // location = ret.getjsonarray("results").getjsonobject(0); // // value of attribute name "formatted_string" // location_string = location.getstring("formatted_address"); // log.d("test", "formattted address:" + location_string); // } catch (jsonexception e1) { // e1.printstacktrace(); // // } } }
change getlocationinfo
method , replace following part.
objectmapper mapper = new objectmapper(); jsonnode array = mapper.readvalue(stringbuilder.tostring(), jsonnode.class); jsonnode object = array.get("results").get(0); string reportkey = object.get("formatted_address").textvalue(); system.out.println(reportkey);
in given code,
jsonnode object = array.get(0);
this return null
(object
contains null
), because result api link return object not array. so, there no 0
element here.
Comments
Post a Comment