一般对于API请求需带上GZip压缩,因为API返回数据大都是JSon串之类字符串,GZip压缩后内容大小大幅降低.
public class GZipRequest extends StringRequest { public GZipRequest(int paramInt, String paramString, Response.Listener<String> paramListener, Response.ErrorListener paramErrorListener) { super(paramInt, paramString, paramListener, paramErrorListener); } public GZipRequest(String paramString, Response.Listener<String> paramListener, Response.ErrorListener paramErrorListener) { super(paramString, paramListener, paramErrorListener); } protected Response<String> parseNetworkResponse( NetworkResponse paramNetworkResponse) { String str1 = ""; try { GZIPInputStream gzipInputStream = new GZIPInputStream( new ByteArrayInputStream(paramNetworkResponse.data)); InputStreamReader inputStreamReader = new InputStreamReader( gzipInputStream); BufferedReader bufferedReader = new BufferedReader( inputStreamReader); while (true) { String str2 = bufferedReader.readLine(); if (str2 == null) break; str1 = str1 + str2; } inputStreamReader.close(); bufferedReader.close(); gzipInputStream.close(); return Response.success(str1, HttpHeaderParser.parseCacheHeaders(paramNetworkResponse), false); } catch (IOException e) { e.printStackTrace(); } return Response.error(new ParseError()); } }
时间: 2024-11-10 01:16:38