我正在使用下面的代码来获取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();
}
}
发布于 2013-07-23 02:06:38
您的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/
发布于 2013-07-22 07:43:49
看起来这里的问题在于HttpPost对象默认将其参数作为x-www-form-urlencoded
发送,但您需要将其作为application/json
发送。这个线程解释了如果你这样做会发生什么:How to use parameters with HttpPost
有几种方法可以解决这个问题。一种方法是在HttpPost对象上设置Content-type头:
request.setHeader("Content-type", "application/json");
另一种我认为更好的方法是使用StringEntity文档here的setContentType方法
entity.setContentType("application/json");
在发送请求之前使用这两行代码中的任何一行都可以解决这个问题。
https://stackoverflow.com/questions/17698378
复制相似问题