使用流的方式去進行post請求解決中文亂碼問題返回xml格式

來源:互聯網
上載者:User

標籤:

/**
* 請求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");
}

使用流的方式去進行post請求解決中文亂碼問題返回xml格式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.