-1

I want to parse the JSON data returned from a google map API. I tried the following code.

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;

    public class JsonReader {

  public static void main(String[] args) throws ParseException  {
        InputStream inputStream = null;
        String json = "";
        try {           
            HttpClient client = new DefaultHttpClient();  
            HttpPost post = new HttpPost("https://maps.googleapis.com/maps/api/geocode/json?address=10115,germany");
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
        } catch(Exception e) {
        }

        try {           
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"),8);
            StringBuilder sbuild = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sbuild.append(line);
            }
            inputStream.close();
            json = sbuild.toString();               
        } catch(Exception e) {
        }


        //now parse
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(json);
        JSONObject jb = (JSONObject) obj;

        //now read
        JSONArray jsonObject1 = (JSONArray) jb.get("results");
        JSONObject jsonObject2 = (JSONObject)jsonObject1.get(0);
        JSONObject jsonObject3 = (JSONObject)jsonObject2.get("geometry");
        JSONObject location = (JSONObject) jsonObject3.get("location");

        System.out.println( "Lat = "+location.get("lat"));
        System.out.println( "Lng = "+location.get("lng"));


    }
}

The source of the above code is the answer of this question. Parse Json array from google maps api in Java

I tried this code and it's showing me this exception

How to solve this??

1
  • If you read the error it says that a class is missing, specifically LogFactory from the org.apache.commons.logging package. There are tons of tutorials online about how to add the jar containing this class to your classpath. Commented May 2, 2016 at 17:06

1 Answer 1

0

Add apache-commons dependencies based on the exception.

Sign up to request clarification or add additional context in comments.

1 Comment

That was a silly mistake. Though thanks for your reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.