[英]Using The Google Maps Geolocation API
我正在使用以下代码来获取lat。 长。 通过提供MCC,MNC,我正在使用Google Maps Geo-location API,但我得到的结果(纬度/经度)与不同的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参数
有几种方法可以解决它。 一种是在HttpPost对象上设置Content-type标头:
request.setHeader("Content-type", "application/json");
另一个,我认为更好,是使用此处记录的StringEntity的setContentType方法
entity.setContentType("application/json");
在发送请求之前使用这些单行中的任何一行都应该解决问题。
您的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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.