• 使用流的方式去进行post请求解决中文乱码问题返回xml格式


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

  • 相关阅读:
    php setcookie(name, value, expires, path, domain, secure) 参数详解
    cookie 和session 的区别详解
    一群猴子排成一圈,按1,2,...n 编号,数到m只,踢出局,直到剩下最后一个猴子是大王
    封装数据库mysql, mysqli
    图片处理类(缩略图)
    封装验证码类
    Java多线程编程(一)
    SpringMVC 自定义拦截资料
    Python包的相对导入时出现错误的解决方法
    TCP协议总结
  • 原文地址:https://www.cnblogs.com/liuying1995/p/5729155.html
Copyright © 2020-2023  润新知