【问题标题】:Using The Google Maps Geolocation API使用 Google Maps Geolocation API
【发布时间】:2013-07-15 22:08:56
【问题描述】:

我正在使用以下代码来获取纬度。长。通过提供MCC、MNC 我为此使用 Google Maps Geo-location API,但对于不同的 MCC/MNC 值,我得到相同的结果(纬度/经度)。即使我请求空白 json,我也会得到相同的结果(纬度/经度)。 我哪里错了?

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

    }

}

【问题讨论】:

  • 你能提供你在代码上使用的导入吗@Tushar

标签: java json geolocation gis cellid


【解决方案1】:

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

{
  "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.
  ]
}

塔对象的格式如下:

{'cellTowers': [
  {
    'cellId': 42,
    'locationAreaCode': 415,
    'mobileCountryCode': 310,
    'mobileNetworkCode': 410,
    'age': 0,
    'signalStrength': -60,
    'timingAdvance': 15
  }
]}

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

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

【讨论】:

  • 另外“callID”的拼写有误,应该是callId。
【解决方案2】:

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

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

request.setHeader("Content-type", "application/json");

另一个我认为更好的方法是使用StringEntity的setContentType方法记录here

entity.setContentType("application/json");

在您发送请求之前使用的这些单行中的任何一个都应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 2020-08-11
    • 2017-12-29
    • 1970-01-01
    • 2014-10-12
    • 2014-11-05
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多