2017-07-25 73 views
1

我想要使用谷歌地圖API獲取地址。在我的項目中,我們使用了Jackson解析器。Java:谷歌反向地理編碼與傑克遜API

我想知道,我怎樣才能得到我期望的結果,即: -

"A10, Dhamidhar Rd, Yashkamal Society, Vasna, Ahmedabad, Gujarat 380007, India" 

我想忽略API中的所有其他領域。由於json文件中的對象太多。 只想獲取:

" "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

謝謝

這是我曾嘗試

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; i < locations.size(); i++){ 
//   GeocodeGetResult object = locations.get(i); 
//   System.out.println(object); 
//  } 


//  for(int i=0; i < 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" and then get the 0th complete object as JSON   
//   location = ret.getJSONArray("results").getJSONObject(0); 
//   // Get the value of the attribute whose name is "formatted_string" 
//   location_string = location.getString("formatted_address"); 
//   Log.d("test", "formattted address:" + location_string); 
//  } catch (JSONException e1) { 
//   e1.printStackTrace(); 
// 
//  } 
    } 
} 
+0

你有什麼試過? – sunkuet02

+0

我編輯了我的問題,檢查它 – Shaun

+0

檢查我的答案,希望它有幫助。它的工作原理是 – sunkuet02

回答

0

更改getLocationInfo方法和替換以下部分。

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); 

在你給出的代碼,

JsonNode object = array.get(0); 

這將返回nullobject包含null),因爲從API鏈接的結果會返回一個對象不是一個數組。因此,這裏沒有0元素。

+0

。謝謝。 – Shaun