/** * Returns the charset specified in the Content-Type of this header, * or the HTTP default (ISO-8859-1) if none can be found. */ public static String parseCharset(Map<String, String> headers) { return parseCharset(headers, HTTP.DEFAULT_CONTENT_CHARSET); } }
/** * Returns the charset specified in the Content-Type of this header, * or the HTTP default (ISO-8859-1) if none can be found. */ public static String parseCharset(Map<String, String> headers) { return parseCharset(headers, HTTP.DEFAULT_CONTENT_CHARSET); } }
/** * 全部转成默认UTF-8 * Returns the charset specified in the Content-Type of this header, * or the HTTP default (ISO-8859-1) if none can be found. */ public static String parseCharset(Map<String, String> headers) { //return parseCharset(headers, HTTP.DEFAULT_CONTENT_CHARSET); return parseCharset(headers, Request.DEFAULT_PARAMS_ENCODING); } }
@Override protected Response<String> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); } }
@Override protected Response<String> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); } }
@Override protected Response<String> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); } }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } T t = mGson.fromJson(parsed, mClazz); //TODO try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return Response.success(t, HttpHeaderParser.parseCacheHeaders(response)); }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); TLog.d(TAG, "response ---> " + jsonString); return Response.success((T) mGson.fromJson(jsonString, mType), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new VolleyError(e)); } }
/** * 数据解析 * @param response Response from the network 网络请求返回数据 * @return */ @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String jsonStr=new String(response.data,HttpHeaderParser.parseCharset(response.headers)); T data=gson.fromJson(jsonStr,mClass); return Response.success(data,HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } }
@Override protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); return Response.success(new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String( response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success( gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException e) { return Response.error(new ParseError(e)); } } }
@Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } }
@Override protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); return Response.success(new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } }
@Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } }
@Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } }
@Override protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); return Response.success(new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } }
@Test public void parseCharset() { assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "ISO-8859-1")); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "utf-8")); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "utf-8")); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
@Test public void parseCharset() { assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "ISO-8859-1")); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers)); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "utf-8")); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); assertEquals("utf-8", HttpHeaderParser.parseCharset(headers, "utf-8")); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
@Test public void parseCaseInsensitive() { long now = System.currentTimeMillis(); Header[] headersArray = new Header[5]; headersArray[0] = new BasicHeader("eTAG", "Yow!"); headersArray[1] = new BasicHeader("DATE", rfc1123Date(now)); headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS)); headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400"); headersArray[4] = new BasicHeader("content-type", "text/plain"); Map<String, String> headers = BasicNetwork.convertHeaders(headersArray); NetworkResponse response = new NetworkResponse(0, null, headers, false); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertEquals("Yow!", entry.etag); assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS); assertEquals(entry.softTtl, entry.ttl); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); } }
@Test public void parseCaseInsensitive() { long now = System.currentTimeMillis(); Header[] headersArray = new Header[5]; headersArray[0] = new BasicHeader("eTAG", "Yow!"); headersArray[1] = new BasicHeader("DATE", rfc1123Date(now)); headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS)); headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400"); headersArray[4] = new BasicHeader("content-type", "text/plain"); Map<String, String> headers = BasicNetwork.convertHeaders(headersArray); NetworkResponse response = new NetworkResponse(0, null, headers, false); Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response); assertNotNull(entry); assertEquals("Yow!", entry.etag); assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS); assertEquals(entry.softTtl, entry.ttl); assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers)); } }