当你途径我的盛放 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 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试