当你途径我的盛放 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 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊