Skip to content

Commit f042123

Browse files
committed
update gzip
1 parent 5fc8b11 commit f042123

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

library/library.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
8989
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
9090
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
91+
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
92+
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
9193
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
9294
</content>
9395
<orderEntry type="jdk" jdkName="Android API 20 Platform" jdkType="Android SDK" />

library/src/main/java/com/android/volley/http/HttpResponse.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.android.volley.http;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
35
import java.util.HashMap;
46
import java.util.Map;
7+
import java.util.zip.GZIPInputStream;
58

69
public class HttpResponse {
710
public static final int SC_MOVED_PERMANENTLY = 301;
@@ -53,4 +56,18 @@ public void setResponseMessage(String responseMessage) {
5356
this.responseMessage = responseMessage;
5457
}
5558

59+
public void checkGzip() throws IOException{
60+
if(httpHeaders != null) {
61+
String acceptEncoding = httpHeaders.get("Content-Encoding");
62+
if(acceptEncoding != null && acceptEncoding.contains("gzip")) {
63+
if(entityFromConnection != null) {
64+
InputStream is = entityFromConnection.getContent();
65+
if(is != null) {
66+
entityFromConnection.setContent(new GZIPInputStream(is));
67+
}
68+
}
69+
}
70+
}
71+
}
72+
5673
}

library/src/main/java/com/android/volley/manager/RequestManager.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.android.volley.RetryPolicy;
1111
import com.android.volley.toolbox.Volley;
1212

13+
import java.util.HashMap;
1314
import java.util.Map;
1415

1516
/**
@@ -20,12 +21,13 @@
2021
*/
2122
public class RequestManager {
2223

23-
private static final int TIMEOUT_COUNT = 10 * 1000;
24+
public static final String ACCEPT_ENCODING = "Accept-Encoding";
25+
public static final String GZIP = "gzip";
2426

27+
private static final int TIMEOUT_COUNT = 10 * 1000;
2528
private static final int RETRY_TIMES = 1;
2629

2730
private volatile static RequestManager INSTANCE = null;
28-
2931
private RequestQueue mRequestQueue = null;
3032

3133
/**
@@ -189,12 +191,19 @@ public LoadController sendRequest(int method, final String url, Object data,
189191
request.setShouldCache(shouldCache);
190192
}
191193

192-
if (headers != null && !headers.isEmpty()) {// add headers if not empty
193-
try {
194-
request.getHeaders().putAll(headers);
195-
} catch (AuthFailureError e) {
196-
e.printStackTrace();
197-
}
194+
//set request header
195+
Map<String, String> requestHeaders = new HashMap<String, String>();
196+
if (headers != null && !headers.isEmpty()) {
197+
requestHeaders.putAll(headers);
198+
}
199+
if(requestHeaders.get(ACCEPT_ENCODING) == null) {
200+
requestHeaders.put(ACCEPT_ENCODING, GZIP);
201+
}
202+
203+
try {
204+
request.getHeaders().putAll(requestHeaders);
205+
} catch (AuthFailureError e) {
206+
e.printStackTrace();
198207
}
199208

200209
RetryPolicy retryPolicy = new DefaultRetryPolicy(timeoutCount,

library/src/main/java/com/android/volley/toolbox/HurlStack.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public HttpResponse performRequest(Request<?> request, Map<String, String> addit
114114
response.addHeader(header.getKey(), header.getValue().get(0));
115115
}
116116
}
117+
response.checkGzip();
117118
return response;
118119
}
119120

@@ -125,7 +126,7 @@ public HttpResponse performRequest(Request<?> request, Map<String, String> addit
125126
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
126127
HttpEntity entity = new HttpEntity();
127128
InputStream inputStream;
128-
129+
129130
try {
130131
inputStream = connection.getInputStream();
131132
} catch (IOException ioe) {

sample/sample.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
87+
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
88+
<excludeFolder url="file://$MODULE_DIR$/build/test-results" />
8789
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
8890
</content>
8991
<orderEntry type="jdk" jdkName="Android API 20 Platform" jdkType="Android SDK" />

0 commit comments

Comments
 (0)