当你途径我的盛放 2017-09-16 09:11 采纳率: 55.6%
浏览 1005

httpClient 工具类异常

工具类代码如下,分页100的情况下没什么问题,分页1000,就会在请求前几页的时候出现json异常,发现返回的数据被截断,换个工具类就没问题了,求解?

    public String doGet(String url) throws UnsupportedEncodingException {
        HttpClient client = new HttpClient(); // 实例化httpClient
        client.getHttpConnectionManager().getParams()
                .setConnectionTimeout(TIME_OUT);
        client.getHttpConnectionManager().getParams().setSoTimeout(TIME_OUT);
        HttpMethod method = new GetMethod(url); //
        String responseContent = "";
        try {
            client.executeMethod(method); // 执行
            InputStream jsonStr;
            jsonStr = method.getResponseBodyAsStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i = -1;
            while ((i = jsonStr.read()) != -1) {
                baos.write(i);
            }
            responseContent = baos.toString();
            jsonStr.close();
            baos.close();
            method.releaseConnection();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (responseContent != null) {
            // 用默认字符编码解码字符串。
            byte[] bs = responseContent.getBytes();
            // 用新的字符编码生成字符串
            return new String(bs, "UTF-8");
        }
        return null;
    }
  • 写回答

1条回答

  • threenewbee 2017-09-16 16:30
    关注

    看下什么异常,是不是超时了,把超时设置大一些。

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法