At this stage, I see two main problems:
- You do not format the URL to accommodate spaces and other friendly characters other than URLs.
- You use POST instead of GET
As with number one, you can do it manually, or you can process it through the java URI class .
As with the second number, you are also using the wrong method to call the url. You should use HttpGet instead of HttpPost.
You can check this using curl:
curl -X POST 'http://maps.google.com/maps/api/geocode/json?address=1600%20Pennsylvania%20Ave%20NW,%20Washington,%20DC&sensor=false'
The POST call above will give you an error where the GET method will not:
curl -X GET 'http://maps.google.com/maps/api/geocode/json?address=1600%20Pennsylvania%20Ave%20NW,%20Washington,%20DC&sensor=false'
Also, instead of using the line builder to read the contents of the body, I find that using apache's own IOUtil class is simpler and easier.
StringWriter writer = new StringWriter(); IOUtils.copy(response.getEntity().getContent(), writer);
However, you will have to import the library.
Finally, make sure you give permission to access the Internet in your manifest.
Tomj
source share