说明:现在很多网站都会在回传数据的时候进行GZIP压缩,我们可以在请求头中申明支持GZIP压缩。可以减轻网络传输压力,Xutils中已经实现。

下面是一个DEMO,便于理解。

private void initGzip() {
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
new Thread(new Runnable() { @Override
public void run() { try {
boolean isGzip = false; // 初始化httpClient对象
DefaultHttpClient httpClient = new DefaultHttpClient(); // 初始化httpGe对象
HttpGet get = new HttpGet("http://mobileif.maizuo.com/city");
// 1.发送请求头:`Accept-Encoding:gzip`
get.addHeader("Accept-Encoding", "gzip"); // HttpGet get = new HttpGet("http://httpbin.org/gzip"); // 发起请求
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
// 2. 取的响应头`Content-Encoding`,判断是否包含Content-Encoding:gzip
Header[] headers = response.getHeaders("Content-Encoding");
for (Header header : headers) {
String value = header.getValue();
if (value.equals("gzip")) {
isGzip = true;
}
} // 3.相应的解压
String result;
HttpEntity entity = response.getEntity();
if (isGzip) {// gzip解压
InputStream in = entity.getContent(); GZIPInputStream gzipIn = new GZIPInputStream(in); // inputStream-->string
result = convertStreamToString(gzipIn);
} else {// 标准解压 // 打印响应结果
result = EntityUtils.toString(entity);
}
System.out.println("result:" + result);
} } catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}).start();
}
}); } public static String convertStreamToString(InputStream is) throws IOException {
try {
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
// BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
// sb.append(line);
sb.append(line).append("\n");
}
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
} catch (Exception e) {
e.printStackTrace();
return "";
} }

Android获取网络数据进行GZIP解压的更多相关文章

  1. android—获取网络数据

    取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子.   大家都知道,网络通信,发送请求有两种 ...

  2. Android -- 获取网络数据并将数据存到本地数据库中

    public static final int downloadDone = 1; // 用户model数组 ArrayList<Loginer> loginers = new Array ...

  3. Android中获取网络数据时的分页加载

    //此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载,    蓝色部分是睡眠时间,自我感觉不用写  ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...

  4. VB6进行GZIP解压&C#进行GZIP压缩和解压

    VB进行GZIP解压的,DLL是系统的,如果没有 [点击下载] Option Explicit 'GZIP API '----------------------------------------- ...

  5. asp.net实现GZip压缩和GZip解压

    最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...

  6. Android获取网络状态

    Android获取网络状态 学习自 https://developer.android.google.cn/reference/android/net/ConnectivityManager http ...

  7. [置顶] 获取网络数据中的数组显示成ListView的简单流程

    首先说一下  这是我自己的个人笔记,如果想看看,不用看细节,可以看流程. 定义一个线程池 ExecutorService pool = Executors.newFixedThreadPool(15) ...

  8. ListView获取网络数据并展示优化练习

    权限: <uses-permission android:name="android.permission.INTERNET"></uses-permission ...

  9. httplib 和 httplib2区别之 gzip解压

    HTTP请求头Accept-encoding: gzip信息告诉服务器,如果它有任何新数据要发送给时,请以压缩的格式发送.如果服务器支持压缩,它将返回由 gzip 压缩的数据并且使用Content-e ...

随机推荐

  1. Registry Workshop(注册表编辑器) V4.6.3 官方中文版

    软件名称: Registry Workshop(注册表编辑器)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.1MB图 ...

  2. 关于PC端与手机端随着手指移动图片位置放生变化的拖拽事件

    当按下鼠标时,图片随鼠标移动松开时图片回到原位 drag("div_id") function drag(node_id){ var node = document.getElem ...

  3. javascript初始笔记

    1.在html中使用点击事件: <button type="button" onclick="myclick()">click me<butt ...

  4. SDK平台三态按钮的实现

    Windows平台提供了丰富的控件,但是在使用中我们不会使用它提供的默认风格,有时候需要对控件进行改写,让它展现出更友好的一面,这次主要是说明三态按钮的实现. 三态按钮指的是按钮在鼠标移到按钮上时显示 ...

  5. JavaScript中prompt()函数的用法。

    定义和用法 prompt()方法用于显示一个带有提示信息,并且用户可以输入的对话框. 语法 prompt(text,defaultText); 参数 描述 text 可选.要在对话框中显示的提示信息( ...

  6. android使用shape做selector按钮按下和弹起的动画

    平时效果:   按下效果: selector代码: <?xml version="1.0" encoding="utf-8"?> <selec ...

  7. CodeForces 703A Mishka and Game

    简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...

  8. Ray Tracing

    Ray Tracing 题目链接:http://codeforces.com/problemset/problem/724/C 拓展欧几里得 //为什么这次C题这么难啊=.= 可以观察到,光线在矩形中 ...

  9. Android的JunitTest

    1.在manifest的配置:首先,manifest的下层级中配置: <instrumentation android:name="android.test.Instrumentati ...

  10. bzoj2052: Pku1777 Vivian

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2052 2052: Pku1777 Vivian Time Limit: 10 Sec  M ...