Java 使用谷歌地图地理定位API

Java 使用谷歌地图地理定位API,java,json,geolocation,gis,cellid,Java,Json,Geolocation,Gis,Cellid,我使用下面的代码来获取lat.long。通过提供MCC、MNC 我使用谷歌地图地理位置API来实现这一点,但我得到的结果(lat/long)与不同的MCC/MNC值相同。即使在请求空白json时,我也会得到相同的结果(lat/long)。 我哪里做错了 public class CellID { public static void main(String[] args) { try{ putDataToServer("https://www.g

我使用下面的代码来获取lat.long。通过提供MCC、MNC 我使用谷歌地图地理位置API来实现这一点,但我得到的结果(lat/long)与不同的MCC/MNC值相同。即使在请求空白json时,我也会得到相同的结果(lat/long)。 我哪里做错了

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

    }

}

这里的问题似乎是HttpPost对象默认以
x-www-form-urlencoded
的形式发送其参数,但您需要以
application/json
的形式发送。此线程解释了如果执行此操作会发生什么:

有两种方法可以解决这个问题。一种是在HttpPost对象上设置内容类型头:

request.setHeader("Content-type", "application/json");
另一个我认为更好的方法是使用StringEntity的setContentType方法


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

您的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
  }
]}
其中塔对象的格式如下:

{
  "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对象是如何变成完整的


而且“callID”的拼写有误,应该是callID。您能提供您在代码@Tushar上使用的导入吗