How to get parsing url on google map to get location and neighborhood info in android

here is my code

public static JSONObject getLocationInfo(String address) { StringBuilder stringBuilder = new StringBuilder(); try { // address = address.replaceAll(" ","%20"); HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); HttpClient client = new DefaultHttpClient(); HttpResponse response; stringBuilder = new StringBuilder(); response = client.execute(httppost); 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) { } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObject; } 

I get json exception in response. please tell me a valid address. I also made mapview (permission api key and android manifest) for gps.

Logcat:

 04-03 16:35:20.548: E/CheckinTask(202): java.net.UnknownHostException: android.clients.google.com 04-03 16:35:20.548: E/CheckinTask(202): at java.net.InetAddress.lookupHostByName(InetAddress.java:506) 04-03 16:35:20.548: E/CheckinTask(202): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 04-03 16:35:20.548: E/CheckinTask(202): at java.net.InetAddress.getAllByName(InetAddress.java:256) 04-03 16:35:20.548: E/CheckinTask(202): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136) 04-03 16:35:20.548: E/CheckinTask(202): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 04-03 16:35:20.548: E/CheckinTask(202): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 04-03 16:35:20.548: E/CheckinTask(202): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359) 04-03 16:35:20.548: E/CheckinTask(202): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 04-03 16:35:20.548: E/CheckinTask(202): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 04-03 16:35:20.548: E/CheckinTask(202): at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:248) 04-03 16:35:20.548: E/CheckinTask(202): at com.google.android.common.http.GoogleHttpClient.executeWithoutRewriting(GoogleHttpClient.java:203) 04-03 16:35:20.548: E/CheckinTask(202): at com.google.android.common.http.GoogleHttpClient.execute(GoogleHttpClient.java:245) 04-03 16:35:20.548: E/CheckinTask(202): at com.google.android.common.http.GoogleHttpClient.execute(GoogleHttpClient.java:313) 04-03 16:35:20.548: E/CheckinTask(202): at com.google.android.gsf.checkin.CheckinTask.sendRequest(CheckinTask.java:254) 04-03 16:35:20.548: E/CheckinTask(202): at com.google.android.gsf.checkin.CheckinTask.doInBackground(CheckinTask.java:150) 04-03 16:35:20.548: E/CheckinTask(202): at com.google.android.gsf.checkin.CheckinService$1.doInBackground(CheckinService.java:221) 04-03 16:35:20.548: E/CheckinTask(202): at com.google.android.gsf.checkin.CheckinService$1.doInBackground(CheckinService.java:214) 04-03 16:35:20.548: E/CheckinTask(202): at android.os.AsyncTask$2.call(AsyncTask.java:185) 04-03 16:35:20.548: E/CheckinTask(202): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 04-03 16:35:20.548: E/CheckinTask(202): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 04-03 16:35:20.548: E/CheckinTask(202): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 04-03 16:35:20.548: E/CheckinTask(202): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 04-03 16:35:20.548: E/CheckinTask(202): at java.lang.Thread.run(Thread.java:1019) 
+1
source share
3 answers

Try the following:

  private String getJSONFile(String URL) { HttpGet httpGet = new HttpGet(URL); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse; StringBuilder stringBuilder = new StringBuilder(); try { httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); InputStream inputStream = httpEntity.getContent(); int c; while ((c = inputStream.read()) != -1) { stringBuilder.append((char) c); } } catch (Exception e) { e.printStackTrace(); } return stringBuilder.toString(); } 
+1
source

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.

0
source

create such a URL

 String address = "yagnik+road,+rajkot"; 

and pass this below url in intent

 http://maps.google.co.in/maps?q=+address 
0
source

All Articles