Jsoup returns error 400 in Google Maps API

I am working on a project that requires me to find the coordinates of some stores on Google Maps. I already have the address of each store.

I played around a bit with the Google Geocoding API and I think this is what I need: all I have to do is connect to the DBMS, retrieve the item_id and address, generate a valid URL for the Geocoding API, and process the JSON data. which he will receive.

I don't understand why, but the URL I created works in my browsers (Chrome 23 and latest Safari, OS X), but doesn't work in Jsoup. I looked at the source of the page in Chrome and it looks like it's absolutely correct HTML. So what is Jsoup doing wrong?

Code snippet (runnable, will give you the same exception):

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class RandomClass {
     public static void main(String args[]) {
     Document doc = null;
     try {
        String url = "http://maps.googleapis.com/maps/api/geocode/json?address=0+164+W+75th+St,+New%20York,+NY+10024&sensor=false";

        String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17";
        doc = Jsoup.connect(url).timeout(60 * 1000).userAgent(ua).get();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

      

}

throws

org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/json; charset=UTF-8, URL=http://maps.googleapis.com/maps/api/geocode/json?address=164+W+75th+St,+New%20York,+NY+10024&sensor=false
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:436)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:393)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:159)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:148)
at asdru.RandomClass.main(RandomClass.java:16)

      

+3


source to share


1 answer


try it

Jsoup.connect(url).ignoreContentType(true).execute().body();

      



OR

Jsoup.connect(url).ignoreContentType(true).timeout(60 * 1000).userAgent(ua).get();

      

+7


source







All Articles