@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)); } }
/** * 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)); } }
public static String checkErrorType(VolleyError error) { String str = ""; if (error instanceof NoConnectionError) { str = ErrorCode.IS_NOT_NETWORK; } else if (error instanceof AuthFailureError) { str = ErrorCode.AUTH_FAILED; } else if (error instanceof TimeoutError) { str = ErrorCode.CONNECTION_TIMEOUT; } else if (error instanceof ParseError) { str = ErrorCode.PARSE_DATA_ERROR; } else if (error instanceof ServerError) { str = ErrorCode.SERVER_ERROR; } else if (error instanceof HttpError) { HttpError httpError = (HttpError) error; str = httpError.getMessage(); if (TextUtils.isEmpty(str)) { str = ErrorCode.REQUEST_ERROR; } } else { str = ErrorCode.REQUEST_ERROR; } return str; }
@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 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)); } }
public static String getMessage(VolleyError error){ if (error instanceof TimeoutError) { return TIME_OUT; } else if(error instanceof NoConnectionError) { return NO_CONNECTION; } else if (error instanceof AuthFailureError) { return AUTH_FAILURE; } else if (error instanceof ServerError) { return SERVER; } else if (error instanceof NetworkError) { return NETWORK; } else if (error instanceof ParseError) { return PARSE; } else{ return DEFAULT; } }
/** * 数据返回 */ @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)); } }
/** * The real guts of parseNetworkResponse. Broken out for readability. */ private Response<Bitmap> doParse(NetworkResponse response) { Bitmap bitmap = null; if (response instanceof StreamBasedNetworkResponse) { InputStream bitmapStream = ((StreamBasedNetworkResponse) response).inputStream; if (bitmapStream != null) { //parse bitmap stream bitmap = doParseStreamSafe(bitmapStream, ((StreamBasedNetworkResponse) response).contentLength); } else { //parse bitmap bytes bitmap = doParseBytes(response.data); } } else { //parse bitmap bytes bitmap = doParseBytes(response.data); } if (bitmap == null) { return Response.error(new ParseError(response)); } else { return Response.success(bitmap, null); } }
@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)); } }
@NonNull private Response<List<SearchEntry>> parseResponse(NetworkResponse response) { try { String html = new String(response.data, parseCharset(response.headers)); Document document = Jsoup.parse(html); Elements elements = document.getElementsByClass("title-detail"); List<SearchEntry> searches = new ArrayList<>(); for (Element element : elements) { Element item = element.child(0); SearchEntry search = new SearchEntry(); search.url = "http://m.guokr.com" + item.attr("href"); search.content = item.html(); search.datetime = element.child(1).child(1).attr("datetime"); searches.add(search); } onParseResult(searches); return Response.success(searches, parseCacheHeaders(response)); } catch (Exception 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)); } }
@Override protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) { // Serialize all decode on a global lock to reduce concurrent heap usage. synchronized (sDecodeLock) { try { return doParse(response); } catch (OutOfMemoryError e) { VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl()); return Response.error(new ParseError(e)); } } }
@Override protected Response<T> parseNetworkResponse(NetworkResponse response) { // TODO Auto-generated method stub try { CommonLog.d("请求成功:statusCode:" + response.statusCode); String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); CommonLog.d(url + "返回:" + jsonStr); return Response.success(JSON.parseObject(jsonStr, mClazz), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { CommonLog.d("请求失败!"); return Response.error(new ParseError(e)); } }
@Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 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<EaseResponse<T>> parseNetworkResponse(NetworkResponse response) { try { String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); EaseResponse<T> allayResponse; if (mTypeToken instanceof EaseStringType && ((EaseStringType) mTypeToken).getActualType().equals(String.class)) { allayResponse = new EaseResponse<>(); allayResponse.setStatusCode(response.statusCode); JsonObject object = new JsonParser().parse(json).getAsJsonObject(); allayResponse.setDescription(parseJsonKey(object, "result_description", "")); // Safe cast to type <T> as both T and data are of type String. //noinspection unchecked allayResponse.setData((T) parseJsonKey(object, "data", null)); } else { allayResponse = new Gson().fromJson(json, mTypeToken.getType()); allayResponse.setStatusCode(response.statusCode); } return Response.success(allayResponse, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return Response.error(new ParseError(e)); } }
@Override protected Response<String> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (Exception je) { return Response.error(new ParseError(je)); } }