如何解析Content-Encoding: gzip服务器返回信息

1、通过HttpClient访问网络信息。

/**
     * 根据给定的url地址访问网络,得到响应内容(这里为GET方式访问)
     * @param url 指定的url地址
     * @return web服务器响应的内容,为<code>String</code>类型,当访问失败时,返回null
     */
    @SuppressWarnings("deprecation")
    public String getWebContent(String url){
        //创建一个Http请求对象
        final HttpGet request = new HttpGet(url);
        HttpParams params = new BasicHttpParams();
        //设置连接超时或响应超时
        //HttpConnectionParams.setConnectionTimeout(params, 3000);
        //HttpConnectionParams.setSoTimeout(params, 5000);
        //创建一个网络访问对象
        final HttpClient httpClient = new DefaultHttpClient(params);

        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    //执行请求参数项
                    HttpResponse response = httpClient.execute(request);
                    //判断是否请求成功
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                        //获取响应信息
                        //noinspection UnnecessaryLocalVariable
                        String content = getResponseString(response.getEntity());
                        return content;
                    } else {
                        //网连接失败,使用Toast显示提示信息
                        Toast.makeText(context, "网络访问失败,请检查您机器的联网设备!", Toast.LENGTH_LONG).show();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //释放网络连接资源
                    httpClient.getConnectionManager().shutdown();
                }
                return null;
            }
        });
        new Thread(task).start();
        try {
            return task.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

2、具体的解析函数。

/**
     * 解析网络响应信息,如果为gzip格式,先解压再转换
     * @param entity 网络返回的HttpEntity信息
     * @return 返回网络响应信息,数据类型为<code>String</code>
     *
     */
    @SuppressWarnings("deprecation")
    private String getResponseString(HttpEntity entity){
        try {
            if ((entity.getContentEncoding() != null)
                    && entity.getContentEncoding().getValue().contains("gzip")) {
                GZIPInputStream gzip = new GZIPInputStream(
                        new ByteArrayInputStream(EntityUtils.toByteArray(entity)));
                InputStreamReader isr = new InputStreamReader(gzip);
                BufferedReader br = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                String temp;
                while((temp = br.readLine()) != null){
                    sb.append(temp);
                    sb.append("\r\n");
                }
                isr.close();
                gzip.close();

                return sb.toString();
            } else {
                return EntityUtils.toString(entity);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

新时代农民工

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sg_knight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值