首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Google Maps地理定位API

使用Google Maps地理定位API
EN

Stack Overflow用户
提问于 2013-07-17 19:16:51
回答 2查看 3.7K关注 0票数 4

我正在使用下面的代码来获取lat.长。通过提供MCC,MNC我正在使用Google Maps Geo-location API来实现这一点,但我得到了不同MCC/MNC值的相同结果(经度/经度)。即使当我请求空白json时,我也得到了相同的结果(lat/long)。我哪里错了?

代码语言:javascript
运行
复制
public class CellID {

    public static void main(String[] args) {
        try{
            putDataToServer("https://www.googleapis.com/geolocation/v1/geolocate?key=mykey",null);
        }
        catch(Throwable throwable){
            System.out.println("Error");
        }
    }

    public static String putDataToServer(String url,JSONObject returnedJObject) throws Throwable
    {

        HttpPost request = new HttpPost(url);

        JSONStringer json = (JSONStringer) new JSONStringer()
        .object() 
         .key("mobileCountryCode").value(504)   
         .key("mobileNetworkCode").value(0)
         .key("locationAreaCode").value(0)
         .key("cellID").value(0)
        .endObject();



        System.out.println("json"+json.toString());

        StringEntity entity = new StringEntity(json.toString(), "UTF-8");


                 request.setEntity(entity); 


        HttpResponse response =null;
        HttpClient httpClient = new DefaultHttpClient();

        try{

            response = httpClient.execute(request); 
        }
        catch(SocketException se)
        {
            throw se;
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        //Displaying the response received.
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
            if (line.startsWith("Auth=")) {
                String key = line.substring(5);
                // Do something with the key
            }

        }
        return response.getEntity().toString();

    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-23 02:06:38

您的JSON请求对象是否完整?我的意思是,你使用的键看起来像是一个单独的“塔”描述的一部分,但这只是更大的请求主体的一部分,应该格式如下:

代码语言:javascript
运行
复制
{
  "homeMobileCountryCode": 310,
  "homeMobileNetworkCode": 410,
  "radioType": "gsm",
  "carrier": "Vodafone",
  "cellTowers": [
   // See the Cell Tower Objects section below.
  ],
  "wifiAccessPoints": [
    // See the WiFi Access Point Objects section below.
  ]
}

其中,塔对象的格式如下:

代码语言:javascript
运行
复制
{'cellTowers': [
  {
    'cellId': 42,
    'locationAreaCode': 415,
    'mobileCountryCode': 310,
    'mobileNetworkCode': 410,
    'age': 0,
    'signalStrength': -60,
    'timingAdvance': 15
  }
]}

我想我错过了你的json对象是如何变成完整的对象的?

https://developers.google.com/maps/documentation/business/geolocation/

票数 1
EN

Stack Overflow用户

发布于 2013-07-22 07:43:49

看起来这里的问题在于HttpPost对象默认将其参数作为x-www-form-urlencoded发送,但您需要将其作为application/json发送。这个线程解释了如果你这样做会发生什么:How to use parameters with HttpPost

有几种方法可以解决这个问题。一种方法是在HttpPost对象上设置Content-type头:

代码语言:javascript
运行
复制
request.setHeader("Content-type", "application/json");

另一种我认为更好的方法是使用StringEntity文档here的setContentType方法

代码语言:javascript
运行
复制
entity.setContentType("application/json");

在发送请求之前使用这两行代码中的任何一行都可以解决这个问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17698378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档