zoukankan      html  css  js  c++  java
  • 使用DataOutputStream输出流的read方法出现读取字节不一致解决办法,本地和测试环境不一致

    之前:

     DataInputStream in = new DataInputStream(connection.getInputStream());
       byte[] b = new byte[in.available()];
    in.read(b);

    之后:

    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"));
    }
    out.flush();
    out.close();
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
    StringBuilder sb=new StringBuilder();
    String str;
    while((str=br.readLine())!=null){
    sb.append(str);
    }
    return sb.toString();
    }

  • 相关阅读:
    二叉树的构造与遍历
    最长公共子序列
    Python爬虫与数据图表的实现
    降维实例之主成分分析
    数据集之转换器以及估计器
    机器学习算法分类以及开发流程
    数据的降维之特征选择及主成分分析
    特征工程之归一化及标准化
    文本tfidf
    文本特征抽取
  • 原文地址:https://www.cnblogs.com/liuying1995/p/5730469.html
Copyright © 2011-2022 走看看