@Override protected com.android.volley.Response<String> parseNetworkResponse(NetworkResponse response) { this.statusCode = response.statusCode; this.responseHeaders = response.headers; /* Get the response data */ try { String json = ""; if (response.data != null) { json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } String log = "%1$s\nResponse code: %2$s\nResponse body: %3$s"; VolleyLog.v(log, getUrl(), statusCode, json); if (statusCode >= 200 && statusCode < 300) { /* Return the parsed result in a response wrapper */ return shouldCache() ? success(json, parseIgnoreCacheHeaders(response)) : success(json, parseCacheHeaders(response)); } else { return error(new ServerError(response)); } } catch (UnsupportedEncodingException e) { return error(new ParseError(e)); } }
@Override protected Response<RemoteResponse> parseNetworkResponse(NetworkResponse response) { RemoteResponse remoteResponse = new RemoteResponse(); if (null != response) { try { remoteResponse.setStatusCode(response.statusCode); remoteResponse.setResponseMessage(HttpStatusNoteMap.getNote(response.statusCode)); remoteResponse.setInterval(response.networkTimeMs); remoteResponse.setHeaders(response.headers); String str = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8")); remoteResponse.setResponse(str); } catch (UnsupportedEncodingException e) { remoteResponse.setResponse(e.getMessage()); } } else { remoteResponse.setStatusCode(-1); remoteResponse.setResponseMessage("Error"); } return Response.success(remoteResponse, HttpHeaderParser.parseCacheHeaders(response)); }
/** * The real guts of parseNetworkResponse. Broken out for readability. */ private Response<Bitmap> doParse(NetworkResponse response) { byte[] data = response.data; BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); Bitmap bitmap = null; if (mMaxWidth == 0 && mMaxHeight == 0) { if (mDecodeConfig != null) { decodeOptions.inPreferredConfig = mDecodeConfig; } bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions); } else { bitmap = ImageUtils.compressBitmap(data, mMaxWidth, mMaxHeight); } if (bitmap == null) { return Response.error(new ParseError()); } else { return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response)); } }
/** * Retorna o Response conforme seu tipo * * @param response * @return * @throws UnsupportedEncodingException * @throws JSONException */ private Response getResponse(NetworkResponse response) throws UnsupportedEncodingException, JSONException { T result; byte[] data = response.data; if (isResponseCompressed(response)) { data = gzipToByte(data); } if (superClass.equals(JSONObject.class)) { result = (T) new JSONObject(convertData(data)); } else if (superClass.equals(JSONArray.class)) { result = (T) new JSONArray(convertData(data)); } else { result = (T) convertData(data); } return Response.success(result, HttpHeaderParser.parseCacheHeaders(response)); }
/** * 通信结束后返回的回调方法. * * @param networkResponse 返回的响应结果对象 */ @Override protected Response<CommonResponse> parseNetworkResponse(NetworkResponse networkResponse) { CommonResponse response = new CommonResponse(); // 获得字符串返回结果 String jsonString; try { App.getInstance().setCookie(networkResponse.headers.get(SET_COOKIE_KEY)); jsonString = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers, OpenApi.CHARSET_UTF8)); Logger.i(jsonString); // 转换返回结果为指定对象 this.doParse(jsonString, mFormat, mTypeToken, response, mRawData); } catch (UnsupportedEncodingException e) { response.setCodeEnum(CodeEnum.DATA_PARSE_ERROR); } return Response.success(response, HttpHeaderParser.parseCacheHeaders(networkResponse)); }
@Override protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 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)); //Allow null if (jsonString == null || jsonString.length() == 0) { return Response.success(null, HttpHeaderParser.parseCacheHeaders(response)); } 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)); } }
@Override public Response<T> parseNetworkResponse(NetworkResponse response) { String parsed; try { String charsetName = HttpHeaderParser.parseCharset(response.headers); parsed = new String(response.data, charsetName); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); e.printStackTrace(); } QyerResponse<T> resp = onResponse(parsed); if (resp.isSuccess()) { Entry entry = HttpHeaderParser.parseCacheHeaders(response); mObjResp = Response.success(resp.getData(), entry); return mObjResp; } else { return Response.error(new VolleyError(resp.getMsg())); } }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { if (mType == null && mJavaClass == null) return Response.error(new ParseError()); try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); T parsedGSON = null; if (mType != null) { parsedGSON = mGson.fromJson(jsonString, mType); } else { parsedGSON = mGson.fromJson(jsonString, mJavaClass); } return Response.success(parsedGSON, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException je) { return Response.error(new ParseError(je)); } }
/** * 强制缓存 * @param response 接收体 * @param maxAge 缓存时间 */ private static Cache.Entry cache(NetworkResponse response, long maxAge) { long now = System.currentTimeMillis(); if (maxAge == 0) maxAge = 60; Map<String, String> headers = response.headers; long serverDate = 0; long softExpire = 0; String serverEtag = null; String headerValue; headerValue = headers.get("Date"); if (headerValue != null) { serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue); } softExpire = now + maxAge * 1000; Cache.Entry entry = new Cache.Entry(); entry.data = response.data; entry.etag = serverEtag; entry.softTtl = softExpire; entry.ttl = entry.softTtl; entry.serverDate = serverDate; entry.responseHeaders = headers; return entry; }
/** * 数据返回 */ @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String strRue = new String(response.data, "UTF-8"); // if (BuildConfig.DEBUG) // Log.d(TAG, xml); if (strRue.contains("error")) { //server返回错误 ServerErrorResult serverError = mGson.fromJson(strRue,ServerErrorResult.class); Response.error(new MyErrorMessage(serverError.getStatus(),serverError.getMessage())); } return Response.success(mGson.fromJson(strRue,mClazz),HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { e.printStackTrace(); return Response.error(new ParseError(e)); } }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { Map<String, String> headers = response.headers; // TODO: 16/6/1 可以处理网络返回头信息 String parsed = ""; if (response.data != null && response.data.length > 0) { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8")); } T map = gson.fromJson(parsed, new TypeToken<T>() { }.getType()); return Response.success(map, HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception je) { return Response.error(new ParseError(je)); } }
@Override protected Response<String> parseNetworkResponse(NetworkResponse response) { try { String resultStr = new String(response.data, HttpHeaderParser.parseCharset(response .headers)); JSONObject jsonObject = new JSONObject(resultStr); if (jsonObject.opt("status").equals("ok")) { JSONObject contentObject = jsonObject.optJSONObject("post"); return Response.success(contentObject.optString("content"), HttpHeaderParser.parseCacheHeaders (response)); } else { return Response.success("error", HttpHeaderParser.parseCacheHeaders(response)); } } catch (Exception e) { e.printStackTrace(); return Response.error(new ParseError(e)); } }
@Override @SuppressWarnings("unchecked") protected Response<T> parseNetworkResponse(NetworkResponse response) { try { if (response != null && response.data != null && response.data.length > 0) { if (ClientUtils.isBasicType(type)) { return (Response<T>) Response.success(new String(response.data, "UTF-8"), HttpHeaderParser.parseCacheHeaders(response)); } else { return Response.success((T) ClientUtils.getJsonReader(type). readValue(response.data), HttpHeaderParser.parseCacheHeaders(response)); } } else { return Response.success(null, HttpHeaderParser.parseCacheHeaders(response)); } } catch (Exception e) { logger.error("JSON parsing error", 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)); Response<JSONArray> success = Response.success(new JSONArray(jsonString), HttpHeaderParser.parseCacheHeaders(response)); if (mProcessor != null) { // do in background... mProcessor.asyncProcess(mContext, success.result); } return success; } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } catch (Exception ex) { return Response.error(new VolleyError(ex)); } }
@Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); Response<JSONObject> success = Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); if (mProcessor != null) { // do in background... mProcessor.asyncProcess(mContext, success.result); } return success; } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } catch (Exception ex) { Log.e(TAG, "process error!", ex); return Response.error(new VolleyError(response)); } }
/** * 通信结束后返回的回调方法. * * @param networkResponse 返回的响应结果对象 */ @Override protected Response<CommonResponse> parseNetworkResponse(NetworkResponse networkResponse) { CommonResponse response = new CommonResponse(); // 获得字符串返回结果 String str; try { str = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers)); Log.d(TAG, "return data=" + str); // 转换返回结果为指定对象 this.doParse(str, mFormat, mTypeToken, response, mRawData); } catch (UnsupportedEncodingException e) { response.setCodeEnum(CodeEnum.DATA_PARSE_ERROR); } return Response.success(response, HttpHeaderParser.parseCacheHeaders(networkResponse)); }
protected final Response<Pair<RequestT, ResponseT>> parseNetworkResponse(NetworkResponse paramNetworkResponse) { try { Class localClass = this.mResponseClass; MessageNano localMessageNano1 = (MessageNano)this.mResponseClass.newInstance(); byte[] arrayOfByte = paramNetworkResponse.data; MessageNano localMessageNano2 = (MessageNano)localClass.cast(MessageNano.mergeFrom$1ec43da(localMessageNano1, arrayOfByte, arrayOfByte.length)); ProtoUtils.logResponse(localMessageNano2, getUrl()); Cache.Entry localEntry = HttpHeaderParser.parseCacheHeaders(paramNetworkResponse); return Response.success(Pair.create(this, localMessageNano2), localEntry); } catch (InstantiationException localInstantiationException) { throw new RuntimeException("Failed to instantiate proto object.", localInstantiationException); } catch (IllegalAccessException localIllegalAccessException) { throw new RuntimeException("Failed to access proto constructor.", localIllegalAccessException); } catch (IOException localIOException) { Log.e("SecureRequest", "Couldn't parse proto response for url=" + getUrl()); } return Response.error(new ParseError(paramNetworkResponse)); }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String( response.data, HttpHeaderParser.parseCharset(response.headers)); Util.sendMessage("/candies/payment/parsing before", json); if (json.indexOf("[") == 0){ json = json.substring(1, json.length() - 2); } Util.sendMessage("/candies/payment/parsing after", json); return Response.success( mGson.fromJson(json, mClass), HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception exception) { return Response.error(new ParseError(exception)); } }
@Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); Response<JSONObject> success = Response.success(new JSONObject( jsonString), HttpHeaderParser.parseCacheHeaders(response)); if (mTask != null) { mTask.asyncProcess(mContext, success.result); } return success; } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } catch (Exception ex) { Log.e(TAG, "process error!", ex); return Response.error(new VolleyError(response)); } }
@Override protected Response<List<String>> parseNetworkResponse(NetworkResponse response) { List<String> hrefStrings; try { hrefStrings = getAllHref(response.data); } catch (Exception e) { e.printStackTrace(); return Response.error(new ParseError(e)); } List<String> result = new LinkedList<String>(); for (String href : hrefStrings) { if (href.contains("【段子】") || href.contains("【喷嚏】")) { String url = HtmlParserUtils.getArg(href, "href", HtmlParserUtils.HREF_SEPARATOR); result.add(HtmlParserUtils.getRelativeUrl(getUrl(), url)); } else if (href.contains("首页")) { break; } } return Response.success(result, HttpHeaderParser.parseCacheHeaders(response)); }
@Override protected Response<Boolean> parseNetworkResponse(NetworkResponse response) { try { String resultStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); JSONObject resultObj = new JSONObject(resultStr); int code = resultObj.optInt("code"); if (code == 0) { return Response.success(true, HttpHeaderParser.parseCacheHeaders(response)); } else { return Response.error(new VolleyError("错误码:" + code)); } } catch (Exception e) { e.printStackTrace(); return Response.error(new VolleyError(e)); } }
@Override protected Response<Boolean> parseNetworkResponse(NetworkResponse response) { try { String resultStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); JSONObject resultObj = new JSONObject(resultStr); String result = resultObj.optString("status"); String error=resultObj.optString("error"); if ( result.equals("ok")) { return Response.success(true, HttpHeaderParser.parseCacheHeaders(response)); } else { return Response.error(new VolleyError("错误原因:" + error)); } } catch (Exception e) { e.printStackTrace(); return Response.error(new VolleyError(e)); } }
@Override protected Response<ArrayList<Joke>> parseNetworkResponse(NetworkResponse response) { try { String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); jsonStr = new JSONObject(jsonStr).getJSONArray("comments").toString(); return Response.success((ArrayList<Joke>) JSONParser.toObject(jsonStr, new TypeToken<ArrayList<Joke>>() { }.getType()), HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { e.printStackTrace(); return Response.error(new ParseError(e)); } }
@Override protected Response<ArrayList<Picture>> parseNetworkResponse(NetworkResponse response) { try { String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); jsonStr = new JSONObject(jsonStr).getJSONArray("comments").toString(); ArrayList<Picture> pictures = (ArrayList<Picture>) JSONParser.toObject(jsonStr, new TypeToken<ArrayList<Picture>>() { }.getType()); return Response.success(pictures, HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { e.printStackTrace(); return Response.error(new ParseError(e)); } }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { Map<String, String> responseHeaders = response.headers; String rawCookies = responseHeaders.get("Set-Cookie"); cookies = rawCookies.substring(0, rawCookies.indexOf(";")); // String cookies = part1; Log.d("sessionid", "sessionid----------------" + cookies); String jsonString = null; try { jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } LogUtil.e(jsonString + "-------------------------------- shr"); return Response.success(mGson.fromJson(jsonString, mClass), 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<Token> parseNetworkResponse(NetworkResponse networkResponse) { String data; try { data = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } Token.Builder token = new Token.Builder(); for (String keyValuePair : TextUtils.split(data, PATTERN_AMPERSAND)) { String[] splitKeyValuePair = TextUtils.split(keyValuePair, PATTERN_EQUALS); if (splitKeyValuePair.length == 2) { addParameterToToken(token, splitKeyValuePair[0], ParameterEncoding.decode(splitKeyValuePair[1])); } else { FWLog.e("Skipping malformed response parameter: %s", keyValuePair); } } return Response.success(token.build(), HttpHeaderParser.parseCacheHeaders(networkResponse)); }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { String json; try{ json = new String(response.data, "UTF-8"); if (BuildConfig.DEBUG) { Log.d("HTTP", getUrl()); Log.d("HTTP", json); } T resultObject = gson.fromJson(json,classType); return Response.success(resultObject,HttpHeaderParser.parseCacheHeaders(response)); } catch (Exception e) { return Response.error(new ParseError(e)); } }
@Override protected Response<String> parseNetworkResponse(NetworkResponse response) { String group = ""; try { String json = new String( response.data, HttpHeaderParser.parseCharset(response.headers)); Pattern pattern = Pattern.compile("name=\"nonce\" value=\"[A-Za-z0-9]{16}\""); Matcher matcher = pattern.matcher(json); if(matcher.find()){ group = matcher.group(); if(!TextUtils.isEmpty(group)){ group = group.substring(20, group.length() -1); } } //name="nonce" value="[A-Za-z0-9]{16}" } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } finally { return Response.success(group, HttpHeaderParser.parseCacheHeaders(response)); } }
protected String getResponseString(NetworkResponse response) throws UnsupportedEncodingException { String responseString = null; String charset = HttpHeaderParser.parseCharset(response.headers); if (mGzipEnabled && isGzipped(response)) { try { Log.d("ExtendedRequest", "decompress"); byte[] data = decompressResponse(response.data); responseString = new String(data, charset); } catch (IOException e) { // it seems that result is not GZIP } } if (responseString == null) { responseString = new String(response.data, charset); } return responseString; }