package smsService;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import smsService.cfg.Globals;
import smsService.sms.StringUtil;
import smsService.util.JWD;
import smsService.util.SecBase64;
/*
* @ClassName GPSToBaiDu
* @description Convert the data of GPS to BaiDu
* @date 2014.10.24
*/
public class GPSToBaiDu {
/*
* 获取HttpClient对象
* @return client
*/
private static HttpClient httpClient(){
BasicHttpParams httpParameters = new BasicHttpParams();
// Set the default socket timeout (SO_TIMEOUT)
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
// in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpClientParams.setCookiePolicy(client.getParams(), CookiePolicy.NETSCAPE);//CookiePolicy.BROWSER_COMPATIBILITY);
return client;
}
/*
* 向百度位置数据发送和接收数据
* @param url 请求链接
* @param client HttpClient
* @param jwdList 原始数据集(GCL-02或者GPS数据)
* @return 经过转换过后的百度坐标
*/
private static List<JWD> send(String url,HttpClient client,List<JWD> jwdList) throws ClientProtocolException, IOException, JSONException{
//使用HTTP的Get方式获取数据
HttpGet get = new HttpGet(url);
//响应
HttpResponse resp = client.execute(get);
//获取实体数据
HttpEntity entity = resp.getEntity();
//读取数据
BufferedReader br = new BufferedReader(new InputStreamReader(entity
.getContent(), "UTF-8"));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
sb.append(result);
result = br.readLine();
}
System.out.println("从百度返回的结果:"+sb.toString());
String res = StringUtil.null2String(sb.toString());
//返回JsonArray
JSONArray js = new JSONArray(res);
//System.out.println("js.toString():"+js.toString());
for(int index = 0;index<js.length();++index){
//获取JSONObject数据对象
JSONObject object = js.getJSONObject(index);
//获取key对应的值
int err = object.getInt("error");
//判断返回结果是否错误,0表示正确
if (0==err) {
//解密
String x = new String(SecBase64.decode(object.getString("x").getBytes()));
String y = new String(SecBase64.decode(object.getString("y").getBytes()));
JWD jwd = new JWD(x,y);
jwdList.add(jwd);
}
}
return jwdList;
}
/*
* 处理单个GPS坐标的转换
* @param jd经度
* @param wd纬度
* @return 返回百度坐标
*/
public static JWD getData(String jd, String wd) {
String wizzer = "";//回调函数
JWD jwd=null;//返回百度坐标数据
try {
HttpClient client = httpClient();
//详见百度坐标转换API文档示例,
//type:0表示GPS经纬度转成百度坐标
// :2表示GCJ-02坐标转成百度坐标
String type="0";
type=StringUtil.null2String(Globals.get("sys.baidu.type"));//Globals.SYS.COM.CONFIG.get("sys.baidu.type")
HttpGet get = new HttpGet("http://api.map.baidu.com/ag/coord/convert?from="+type +"&to=4&x=" + jd + "&y=" + wd + "&callback="+wizzer);
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity
.getContent(), "UTF-8"));
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
sb.append(result);
result = br.readLine();
}
System.out.println("从百度返回的结果:"+sb.toString());
String res = StringUtil.null2String(sb.toString());
//创建JSONObject
JSONObject js = new JSONObject(res);
String err = js.getString("error");
//判断返回结果是否错误,0表示正确
if ("0".equals(err)) {
jwd=new JWD();//创建对象
//System.out.println("js.toString():"+js.toString());
//解密
String x = new String(SecBase64.decode(js.getString("x").getBytes()));
String y = new String(SecBase64.decode(js.getString("y").getBytes()));
//存储百度坐标值
jwd.setX(x);
jwd.setY(y);
}else{
return null;
}
return jwd;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*
* 批量处理GPS坐标
* @param list原始数据集合
*/
public static List<JWD> getDataArray(List<JWD> list) {
String wizzer = "";//回调函数
List<JWD> jwdList=new ArrayList<JWD>();//返回百度坐标数据
int maxCnt = 20;//每次发送请求最多能处理20条数据的转换
try {
HttpClient client = httpClient();
//详见百度坐标转换API文档示例,
//type:0表示GPS经纬度转成百度坐标
// :2表示GCJ-02坐标转成百度坐标
String type="0";
type=StringUtil.null2String(Globals.get("sys.baidu.type"));//Globals.SYS.COM.CONFIG.get("sys.baidu.type")
String url = "http://api.map.baidu.com/ag/coord/convert?from="+type+"&to=4&mode=1&x=";
/*
* 转换一组数据
* "http://api.map.baidu.com/ag/coord/convert?from="+type+"&to=4&mode=1&x=1,2,3&y=4,5,6&callback="+wizzer;
*/
String jds = "";
String wds= "";
for(int index = 0;index<list.size();++index){
//每次最多只能处理20条数据
if(index%maxCnt==0 && index!=0){
jds = jds.substring(0,jds.length()-1);
wds = wds.substring(0,wds.length()-1);
String s = url +jds+"&y="+wds+"&callback="+wizzer;
System.out.println(url);
jwdList = send(s,client,jwdList);
jds = "";
wds = "";
}
jds+=list.get(index).getX()+",";
wds+=list.get(index).getY()+",";
//处理mode20的余数
if(index== list.size()-1){
jds = jds.substring(0,jds.length()-1);
wds = wds.substring(0,wds.length()-1);
String s = url +jds+"&y="+wds+"&callback="+wizzer;
jwdList = send(s,client,jwdList);
jds = "";
wds = "";
}
}
return jwdList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*
* 测试
* main函数
*/
public static void main(String args[]) {
//构造批量数据
List<JWD> list = new ArrayList<JWD>();
list.add(new JWD(116.3786889372559,39.90762965106183));
l
- 1
- 2
- 3
前往页