通过post方法,将CID,LAC,MCC,MNC等信息post到 https://www.googleapis.com/geolocation/.....问题

ftp2010_fly 2013-06-08 04:59:22
请教各位大侠,
我通过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;
}
...全文
1380 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Clement-Xu 2013-07-11
  • 打赏
  • 举报
回复
找到方法了,要加上这两行: post.setHeader("Accept", "application/json"); post.setHeader("Content-type", "application/json");
Clement-Xu 2013-07-10
  • 打赏
  • 举报
回复
楼主,我也是同样的错误,找到解决方法了吗?
bobmanb 2013-06-27
  • 打赏
  • 举报
回复
看上去说这是个商用系统,要付钱的吧
ftp2010_fly 2013-06-08
  • 打赏
  • 举报
回复
请问大家如何解决,谢谢

6,721

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 Google技术社区
社区管理员
  • Google技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧