使用curl测试GeolocationAPI接口
很多朋友在使用google GeolocationAPI 接口测试基站定位,测试时需要往接口http://www.google.com/loc/json提交json格式的数据,json格式参数比较多,在IDE里测试起来也比较麻烦,有时因为一个语法错误不得不排查很长时间。
这里ant推荐一个比较简单的方法来测试json数据格式是否正确:使用curl测试。
curl是一个利用URL语法在命令行方式下工作的文件传输工具。使用curl来提交http GET/POST数据很是方便。curl是Unix/Linux下常用的工具,也有windows版本。
GeolocationAPI 的详细语法这里就不介绍了。
例如我们想通过http://www.google.com/loc/json查询LAC:14556 ,CELLID:36525 的基站位置。根据GeolocationAPI 里提到的语法,提交的数据应该是这样的格式:
{
"version": "1.1.0" ,
"host": "maps.google.com",
"access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",
"home_mobile_country_code": 460,
"home_mobile_network_code":0,
"address_language": "zh_CN",
"radio_type": "gsm",
"request_address": true ,
"cell_towers":[
{
"cell_id":36526,
"location_area_code":14556,
"mobile_country_code":460,
"mobile_network_code":0,
"timing_advance":5555
}
]
}
下面我们通过curl测试该格式是否正确。
命令格式: curl -d ‘post的数据’ http://www.google.com/loc/json
这里只需要用到curl的-d参数,-d后面跟的是post的数据内容
在命令行下输入” curl -d ‘ “后回车,粘贴上面的json格式数据回车,再输入” ‘ http://www.google.com/loc/json”回车,就可以看到google的返回结果了。
windows版curl和linux有点小区别,因为windows cmd对 ” ‘ 符号处理和linux不一样,windows版curl发送时要把 ” 换成 \”,而且整个命令要写成一行
如下所示:
curl -d "{\"version\": \"1.1.0\" ,\"host\": \"maps.google.com\",\"access_token\": \"2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe\",\"home_mobile_country_code\": 460,\"home_mobile_network_code\":0,\"address_language\": \"zh_CN\",\"radio_type\": \"gsm\",\"request_address\": true ,\"cell_towers\":[{\"cell_id\":36526,\"location_area_code\":14556,\"mobile_country_code\":460,\"mobile_network_code\":0,\"timing_advance\":5555}]}" http://www.google.com/loc/json
通过curl可以方便的测试各种json参数组合。

哦哦哦,好的,找个本本记下。:)很有用。
用这个测试,报告json parsing error哦,我用的是下载的windows 版本的curl工具。
搞定了。
您好,我试图用向那个地址发送我的json请求包,发现怎么发都是返回空值,只有cell_towers为空时才会获得一个貌似是默认的数据包,用我自己的CID和LAC就不行,请问这是为什么呢
@暗月
贴一下你的发送部分代码看看
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
“http://www.google.com/loc/json”);
JSONObject holder = new JSONObject();
JSONArray array = new JSONArray();
JSONObject data = new JSONObject();
// data.put(“cell_id”, cid); // 4474
// data.put(“location_area_code”, cid);// 25070
// data.put(“mobile_country_code”, mcc);// 460
// data.put(“mobile_network_code”, mnc);// 0
data.put(“cell_id”, 4474); // 4474
data.put(“location_area_code”, 25070);// 25070
data.put(“mobile_country_code”, 460);// 460
data.put(“mobile_network_code”, 0);// 0
data.put(“timing_advance”, 5555);
array.put(data);
holder.put(“cell_towers”, array);
holder.put(“address_language”, “en-us”);
// holder.put(“radio_type”, “gsm”);
holder.put(“request_address”, true);
holder.put(“version”, “1.1.0″);
StringEntity se = new StringEntity(holder.toString());
Log.e(“Location send”, holder.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
Log.e(“Locaiton reseive”, result);
sb.append(result);
result = br.readLine();
}
result_location = sb.toString();
data = new JSONObject(sb.toString());
data = (JSONObject) data.get(“location”);
la = (Double) data.get(“latitude”);
lo = (Double) data.get(“longitude”);
mHandler.sendEmptyMessage(101);
我想知道这两句的输出结果
Log.e(“Location send”, holder.toString());
Log.e(“Locaiton reseive”, result);
另外,还是加入群68287173 讨论方便些
用微软的HttpDebug工具Fiddler更好用
非常容易构造请求,还可以抓http的包
@kaison111
楼上的,能否给我发一下你的工具。谢谢!邮箱:1159857006@qq.com
请问能给我点提示吗??QQ:1159857006 再次谢过.
data.put(“cell_id”, 4474); —-> 他要求的格式為字串喔 , 不是數字 , 改成 “4474″或許就沒問題了
{
“version”: “1.1.0″ ,
“host”: “maps.google.com”,
“access_token”: “2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe”,
“home_mobile_country_code”: 460,
“home_mobile_network_code”:0,
“address_language”: “zh_CN”,
“radio_type”: “gsm”,
“request_address”: true ,
“cell_towers”:[
{
"cell_id":36526,
"location_area_code":14556,
"mobile_country_code":460,
"mobile_network_code":0,
"timing_advance":5555
}
]
}
问下 这串数据怎么发送出去,我目前是在getlocation()抓包的那个里面发现的前面的一点包头是:
POST /loc/json HTTP/1.1
Accept: */*
Content-Type: application/json
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 163
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; msn OptimizedIE8;ZHCN; AskTB5.6)
Host: http://www.google.com
Connection: Keep-Alive
这个后面加刚才发送的那些数据的,结果发送出去,接收回来的抓包,发现是json parsing error
ANT 求一下正确的发送数据格式,不用CURL这个工具,就纯粹的发送数据包给GOOGLE服务器。
在线坐等,顺便QQ群,加我一下
@六月的雨
已加
能否加我下群 qq:911619252 谢谢!
我用curl 测试一直没有通过 不知道格式怎么写 试过很多种方式了!
我也想加入这个群,拒绝我加入?!