Use stream to go to post request to solve Chinese garbled problem return XML format

Source: Internet
Author: User

/**
* Request Post
* @Title: Gethttpurlconnection
* @Description: TODO (here is a word describing the effect of this method)
* @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);
Shortest
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"));
}

Call Mode:

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]));//conversion to 84 coordinate system
Gps gps1= positionutil.gcj_to_gps84 (double.valueof (split02[1]), double.valueof (split02[0]));//conversion to 84 coordinate system
Gps gps2= positionutil.gcj_to_gps84 (double.valueof (split03[1]), double.valueof (split03[0]));//conversion to 84 coordinate system
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);

Returns data in XML format

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");
}

Use stream to go to post request to solve Chinese garbled problem return XML format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.