/**
* 请求post
* @Title: getHttpURLConnection
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param: @param url1
* @param: @param paream
* @param: @return
* @param: @throws IOException
* @param: @throws JSONException
* @return: String
* @throws
* @author ecar
* @Date 2016-7-23 下午03:08:59
*/
private String getHttpURLConnection(String url1,String paream) throws IOException, JSONException
{
Long deviceId=1l;
URL url = new URL(url1);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/json;charset=utf-8");
connection.setConnectTimeout(80*1000);
connection.setReadTimeout(80*1000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeLong(deviceId);
//最短
JSONArray array = new JSONArray(paream);
out.writeShort(array.length());
for(int i=0;i<array.length();i++){
JSONObject obj = array.getJSONObject(i);
out.write(obj.getString("time").getBytes("UTF8"));
double d = obj.getDouble("lon");
int lon = (int)(d * 1000000);
out.writeInt(lon);
d = obj.getDouble("lat") ;
int lat = (int)(d * 1000000);
out.writeInt(lat);
out.writeFloat((float)obj.getDouble("speed"));
out.writeFloat((float)obj.getDouble("direc"));
}

调用方式:

String ss = "[" ;
String date = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date());
Gps gps= PositionUtil.gcj_To_Gps84(Double.valueOf(split[1]), Double.valueOf(split[0]));//转化为84的坐标系
Gps gps1= PositionUtil.gcj_To_Gps84(Double.valueOf(split02[1]), Double.valueOf(split02[0]));//转化为84的坐标系
Gps gps2= PositionUtil.gcj_To_Gps84(Double.valueOf(split03[1]), Double.valueOf(split03[0]));//转化为84的坐标系
ss+="{\"time\":\""+date+"\",\"lon\":"+gps.getWgLon()+",\"lat\":"+gps.getWgLat()+",\"speed\":20,\"direc\":0},";
ss+="{\"time\":\""+date+"\",\"lon\":"+gps1.getWgLon()+",\"lat\":"+gps1.getWgLat()+",\"speed\":30,\"direc\":0},";
ss+="{\"time\":\""+date+"\",\"lon\":"+gps2.getWgLon()+",\"lat\":"+gps2.getWgLat()+",\"speed\":30,\"direc\":0}";
ss+="]";
String param=getHttpURLConnection(uriAPI+"/RouteTrafficInfoSearch?bizcode=fBAym29ZX11kVKbuQVnJ",ss);
System.out.println(param);

返回xml格式的数据

out.flush();
out.close();
DataInputStream in = new DataInputStream(connection.getInputStream());
int len = in.available();
byte[] b = new byte[len];
in.read(b);
// System.out.println(new String(b,"utf-8"));
return new String(b,"utf-8");
}

posted on 2016-08-02 14:28  小莹莹  阅读(792)  评论(0编辑  收藏  举报