JSON

Android基站定位(2)

字号+ 作者:H5之家 来源:H5之家 2017-07-31 11:01 我要评论( )

request URL: Request Method:POST Status Code:200 OK Request Headersview source Accept:application/json, text/javascript, */*; q=0.01 Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflat

request URL:
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:191
Content-Type:application/json; charset=UTF-8
Cookie:bdshare_firstime=1356366713546; JSESSIONID=68243935CD3355089CF07A3A22AAB372
Host:
Origin:
Referer:
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
X-Requested-With:XMLHttpRequest
Request Payload
{"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0, "mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}
Response Headersview source
Content-Type:application/json
Date:Sat, 03 Jan 2013 14:03:15 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

3、用JAVA代码具体实现

/**
* 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息
*/
public String httpPost(String url, String jsonCellPos) throws IOException{
byte[] data = jsonCellPos.toString().getBytes();
URL realUrl = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();
httpURLConnection.setConnectTimeout(6 * 1000);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
httpURLConnection.setRequestProperty("Host", "");
httpURLConnection.setRequestProperty("Referer", "");
httpURLConnection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");
httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
httpURLConnection.setRequestProperty("Host", "");
DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());
outStream.write(data);
outStream.flush();
outStream.close();
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpURLConnection.getInputStream();
return new String(read(inputStream));
}
return null;
}

4、读取返回的JSON数据流代码:

/**
* 读取IO流并以byte[]形式存储
* @param inputSream InputStream
* @return byte[]
* @throws IOException
*/
public byte[] read(InputStream inputSream) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = inputSream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inputSream.close();
return outStream.toByteArray();
}

三、请求参数及返回结果的JSON形式:

1、请求的JSON参数值:

{
"cell_towers":
[
{
"mobile_network_code":"1",
"location_area_code":"43018",
"cell_id":"11152773",
"age":0,
"mobile_country_code":"460"
}
],
"host":"maps.google.com",
"version":"1.1.0"
}

2、返回的JSON结果值:

{
"location":
{
"latitude":"31.211389541625977",
"longitude":"121.60332489013672",
"address":
{"city":
"上海市浦东新区居里路432号;浦东新区光启安老院、第一三共制药上海公司、SUNPLUS[附近]",
"country":"",
"country_code":"",
"county":"",
"postal_code":"",
"region":"",
"street":"",
"street_number":""
}
},
"access_token":"dummytoken"
}

四、完整代码及所需权限:

Java代码:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android实战中与Json数据、Java

    Android实战中与Json数据、Java

    2017-07-29 14:03

  • Android的Gradle技巧 4.3排除任务

    Android的Gradle技巧 4.3排除任务

    2017-07-06 08:02

  • ubuntu移植jsoncpp到Android平台

    ubuntu移植jsoncpp到Android平台

    2017-07-04 18:00

  • Android 解析JSON数据格式

    Android 解析JSON数据格式

    2017-06-26 18:04

网友点评
"