package com.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import android.content.Context;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
public class Location {
public static String LOCATIONS_URL = "http://www.google.com/loc/json";
public static String getLocations(Context context) {
// generate json request
String jr = generateJsonRequest(context);
try {
DefaultHttpClient client = new DefaultHttpClient();
StringEntity entity = new StringEntity(jr);
HttpPost httpost = new HttpPost(LOCATIONS_URL);
httpost.setEntity(entity);
HttpResponse response = client.execute(httpost);
String locationsJSONString = getStringFromHttp(response.getEntity());
return extractLocationsFromJsonString(locationsJSONString);
} catch (ClientProtocolException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
} catch (Exception e) {
//e.printStackTrace();
}
return null;
}
private static String extractLocationsFromJsonString(String jsonString) {
String country = "";
String region = "";
String city = "";
String street = "";
String street_number = "";
double latitude = 0.0;
double longitude = 0.0;
//"accuracy":901.0
double accuracy = 0.0;
try {
JSONObject jo = new JSONObject(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 返回
private static String getStringFromHttp(HttpEntity entity) {
StringBuffer buffer = new StringBuffer();
try {
// 获取输入流
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent()));
// 将返回的数据读到buffer中
String temp = null;
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
} catch (IllegalStateException e) {
//e.printStackTrace();
} catch (IOException e) {
//e.printStackTrace();
}
return buffer.toString();
}
private static String generateJsonRequest(Context context) {
TelephonyManager manager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
List<NeighboringCellInfo> cellList = manager.getNeighboringCellInfo();
if (cellList.size() == 0)
return null;
JSONStringer js = new JSONStringer();
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 = new JSONArray();
for (NeighboringCellInfo info : cellList) {
JSONObject c = new JSONObject();
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();
return null;
}
return js.toString().replace("true", "True");
}
}
分享到:
相关推荐
java 通过发送json,post请求,返回json数据的方法 java 通过发送json,post请求,返回json数据的方法
java代码发送JSON格式的httpPOST请求
java发送http/https请求(get/post)代码,java 通过发送json,post请求,返回json数据的方法 java 通过发送json,post请求,返回json数据的方法
使用Java获取制定接口的数据,使用了httpserverletrequest类。
java http-post json格式客户端请求代码
java发送JSON格式的http通讯的post请求
js ajax发起post请求,发送json数据。平台接收并返回json数据,html显示返回结果
java调用json参数的webservice 涉及技术: JAVA JSON WEBSERVICE
C#开发webservice接口,对客户端post服务的Json数据进行接收反馈 接收到的数据流转换成string类型,有其他需求对json解析,自己写个解析去查询下. 然后反馈json发送给请求端。
本篇文章主要介绍了java模拟post请求发送json的例子,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://javasam.iteye.com/blog/2117845
java的get和post请求,获取json的工具类,https时会存在ssl校验的问题,工具会自动去除ssl校验。
用java编写了http Post的请求代码,通过发送请求的 URL,获取远程资源的响应结果,入参为json字符串。使用到httpPost,CloseableHttpClient
web服务的ajax请求数据直接调用javaDAO类返回JSON数据。取值方便。快捷
Java与json之间的转换、通过发送post请求访问第三方接口、使用freemarker模拟生成静态sql、wsdl2Java生成客户代码
Android Studio实现POST请求,每个步骤都有注释,比较适合萌新了解如果通过POST方式发起网络请求。
http json 模拟请求工具,可以发送http请求,并且可选择发送请求的类型,可以发送json数据。
本项目使用了jQuery的get/post两种方法提交Json格式的数据到后台,后台再把Json格式的数据传到前台。实现了JSON字符串和JSON对象之间的转换。
POST请求,请求体JSON,一步到位。 StringEntity entity = new StringEntity(jsonString, "UTF-8"); // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中 httpPost.setEntity(entity); ...