请教各位大侠,
我通过post方法,将CID,LAC,MCC,MNC等信息post到 https://www.googleapis.com/geolocation/v1/geolocate?key=“我自己申请的key”,结果,返回的错误信息如下,经查,错误信息说明是JSON格式不对,但是,我是按照google文档上的格式来写的。请问大家如何解决?
1)返回的错误信息
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
2)我的实现:
public class SCell{
public int MCC;
public int MNC;
public int LAC;
public int CID;
}
private SItude getItude1(SCell cell) throws Exception {
SItude itude = new SItude();
/** 采用Android默认的HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用POST方法 */
HttpPost post = new HttpPost("https://www.googleapis.com/geolocation/v1/geolocate?key=XXXXXXXXXXXXXXXXXX");
try {
/** 构造POST的JSON数据 */
JSONObject holder = new JSONObject();
holder.put("homeMobileCountryCode", cell.MCC);
holder.put("homeMobileNetworkCode", cell.MNC);
holder.put("radioType", "gsm");
holder.put("carrier", "cmcc");
JSONObject tower = new JSONObject();
tower.put("cellId", cell.CID);
tower.put("locationAreaCode", cell.LAC);
tower.put("mobileCountryCode", cell.MCC);
tower.put("mobileNetworkCode", cell.MNC);
tower.put("age", 0);
tower.put("signalStrength", -60);
tower.put("timingAdvance", 15);
JSONArray towerarray = new JSONArray();
towerarray.put(tower);
holder.put("cellTowers", towerarray);
StringEntity query = new StringEntity(holder.toString());
post.setEntity(query);
/** 发出POST数据并获取返回数据 */
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null) {
strBuff.append(result);
}
/** 解析返回的JSON数据获得经纬度 */
JSONObject json = new JSONObject(strBuff.toString());
JSONObject subjosn = new JSONObject(json.getString("location"));
itude.latitude = subjosn.getString("latitude");
itude.longitude = subjosn.getString("longitude");
Log.i("Itude", itude.latitude + itude.longitude);
} catch (Exception e) {
Log.e(e.getMessage(), e.toString());
throw new Exception("获取经纬度出现错误:"+e.getMessage());
} finally{
post.abort();
client = null;
}
return itude;
}