java http请求 json数据_java http 请求之post json数据

packagecom.utils;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.List;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importorg.json.JSONStringer;importandroid.content.Context;importandroid.telephony.NeighboringCellInfo;importandroid.telephony.TelephonyManager;publicclassLocation {publicstaticString LOCATIONS_URL="http://www.google.com/loc/json";publicstaticString getLocations(Context context) {//generate json requestString jr=generateJsonRequest(context);try{

DefaultHttpClient client=newDefaultHttpClient();

StringEntity entity=newStringEntity(jr);

HttpPost httpost=newHttpPost(LOCATIONS_URL);

httpost.setEntity(entity);

HttpResponse response=client.execute(httpost);

String locationsJSONString=getStringFromHttp(response.getEntity());returnextractLocationsFromJsonString(locationsJSONString);

}catch(ClientProtocolException e) {//e.printStackTrace();}catch(IOException e) {//e.printStackTrace();}catch(Exception e) {//e.printStackTrace();}returnnull;

}privatestaticString extractLocationsFromJsonString(String jsonString) {

String country="";

String region="";

String city="";

String street="";

String street_number="";doublelatitude=0.0;doublelongitude=0.0;//"accuracy":901.0doubleaccuracy=0.0;try{

JSONObject jo=newJSONObject(jsonString);

JSONObject location=(JSONObject) jo.get("location");

latitude=(Double) location.get("latitude");

longitude=(Double) location.get("longitude");

accuracy=(Double) location.get("accuracy");

JSONObject address=(JSONObject) location.get("address");

country=(String) address.get("country");

region=(String) address.get("region");

city=(String) address.get("city");

street=(String) address.get("street");

street_number=(String) address.get("street_number");

}catch(JSONException e) {//e.printStackTrace();}return"("+latitude+","+longitude+")\t"+country+region+city+street+street_number+"\t"+"精确度:"+accuracy;

}//获取所有的网页信息以String 返回privatestaticString getStringFromHttp(HttpEntity entity) {

StringBuffer buffer=newStringBuffer();try{//获取输入流BufferedReader reader=newBufferedReader(newInputStreamReader(

entity.getContent()));//将返回的数据读到buffer中String temp=null;while((temp=reader.readLine())!=null) {

buffer.append(temp);

}

}catch(IllegalStateException e) {//e.printStackTrace();}catch(IOException e) {//e.printStackTrace();}returnbuffer.toString();

}privatestaticString generateJsonRequest(Context context) {

TelephonyManager manager=(TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

ListcellList=manager.getNeighboringCellInfo();if(cellList.size()==0)returnnull;

JSONStringer js=newJSONStringer();try{

js.object();

js.key("version").value("1.1.0");

js.key("host").value("maps.google.com");

js.key("home_mobile_country_code").value(460);

js.key("home_mobile_network_code").value(0);

js.key("radio_type").value("gsm");

js.key("request_address").value(true);

js.key("address_language").value("zh_CN");

JSONArray ct=newJSONArray();for(NeighboringCellInfo info : cellList) {

JSONObject c=newJSONObject();

c.put("cell_id", info.getCid());

c.put("location_area_code", info.getLac());

c.put("mobile_country_code",460);

c.put("mobile_network_code",0);

c.put("signal_strength", info.getRssi());//获取邻居小区信号强度ct.put(c);

}

js.key("cell_towers").value(ct);

js.endObject();

}catch(JSONException e) {//e.printStackTrace();returnnull;

}returnjs.toString().replace("true","True");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值