如何使用默认语言(fx.:Chinese)以外的语言获取Google Geocoding API的响应?

时间:2012-01-11 15:40:18

标签: android location gson

我已经多次通过解析包含中文地址信息的Google json来获取地址。但是当我在我的手机中显示地址时,它全部都是英文的。

我从下面的网址获取了json。 http://maps.googleapis.com/maps/api/geocode/json?address=wuhan&sensor=false

修改 在答案的帮助下,我将与工作解决方案分享:

            StringBuffer sb=new StringBuffer();
            sb.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=").append(latStr).append(',').append(lonStr).append("&sensor=false&Accept-Language:zh-CN");
            String url=sb.toString();
            HttpClient httpClient=new DefaultHttpClient();

            String responseData="";
            try {
                HttpResponse response=httpClient.execute(new HttpGet(url));
                response.addHeader("Accept-Language", "zh-CN");
                HttpEntity entity=response.getEntity();
                BufferedReader bf=new BufferedReader(new InputStreamReader((entity.getContent()),"UTF-8"));
                String line="";
                while((line=bf.readLine())!=null){
                    responseData=responseData+line;
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:2)

只需将Accept-Language添加到reaquest,因为默认情况下,google API会返回英文信息 FX: Accept-Language: zh-CN

编辑(coz OP徘徊):

如果您使用java.net.HttpURLConnection connection;,请使用:

connection.setRequestProperty ( "Accept-Language", "zh-CN");

如果org.apache.http.client.methods.HttpGet request;则:

request.addHeader("Accept-Language", "zh-CN");

我刚刚在fiddler2

中对其进行了测试

GET /maps/api/geocode/json?address=wuhan&sensor=false HTTP/1.0
Host: maps.googleapis.com
Accept-Language: zh-CN

我得到了

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "武汉",
               "short_name" : "武汉",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "湖北省",
               "short_name" : "湖北省",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国湖北省武汉市",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 31.36126030,
                  "lng" : 115.08257280
               },
               "southwest" : {
                  "lat" : 29.96907670,
                  "lng" : 113.70228110
               }
            },
            "location" : {
               "lat" : 30.5930870,
               "lng" : 114.3053570
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 30.78745989999999,
                  "lng" : 114.6189880
               },
               "southwest" : {
                  "lat" : 30.34877210,
                  "lng" : 113.9817810
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}