Java Code Examples for com.android.volley.Response#error()

The following examples show how to use com.android.volley.Response#error() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RVImageRequest.java    From RestVolley with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: GsonRequest.java    From VolleyX with Apache License 2.0 6 votes vote down vote up
@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));
    }
}
 
Example 3
Source File: RVImageRequest.java    From RestVolley with Apache License 2.0 5 votes vote down vote up
@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));
        }
    }
}
 
Example 4
Source File: JsonObjectRequest.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@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));
    }
}
 
Example 5
Source File: JsonRequest.java    From openshop.io-android with MIT License 5 votes vote down vote up
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        requestStatusCode = response.statusCode;
        if (BuildConfig.DEBUG)
            Timber.d("%s URL: %s. ResponseCode: %d", this.getClass().getSimpleName(), requestUrl, response.statusCode);
    } catch (Exception e) {
        return Response.error(new ParseError(e));
    }
    return super.parseNetworkResponse(response);
}
 
Example 6
Source File: ImageRequest.java    From device-database with Apache License 2.0 5 votes vote down vote up
@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));
        }
    }
}
 
Example 7
Source File: JsonArrayRequest.java    From device-database with Apache License 2.0 5 votes vote down vote up
@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));
    }
}
 
Example 8
Source File: JsonObjectRequest.java    From device-database with Apache License 2.0 5 votes vote down vote up
@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));
    }
}
 
Example 9
Source File: ImageRequest.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@Override protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) {
    // Serialize all decode on a global lock to reduce concurrent heap usage.
    // 解析 ImageRequest 的网络请求这块进行加锁,避免 OOM
    synchronized (sDecodeLock) {
        try {
            return doParse(response);
        } catch (OutOfMemoryError e) {
            // 发生 OOM,返回一个只带有 error 的 Response
            VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl());
            return Response.error(new ParseError(e));
        }
    }
}
 
Example 10
Source File: VolleyMultipartRequest.java    From TvAppRepo with Apache License 2.0 5 votes vote down vote up
@Override
protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {
    try {
        return Response.success(
                response,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (Exception e) {
        return Response.error(new ParseError(e));
    }
}
 
Example 11
Source File: StringRequestGet.java    From renrenpay-android with Apache License 2.0 5 votes vote down vote up
@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 (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 12
Source File: HTMobileClient.java    From live-app-android with MIT License 5 votes vote down vote up
@Override
protected Response<JsonObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String json = new String(
                response.data, HttpHeaderParser.parseCharset(response.headers));

        return Response.success(
                gson.fromJson(json, JsonObject.class), HttpHeaderParser.parseCacheHeaders(response));

    } catch (UnsupportedEncodingException | JsonSyntaxException e) {
        HTLogger.e(TAG, "parseNetworkResponse: ", e);
        ParseError parseError = new ParseError(response);
        return Response.error(parseError);
    }
}
 
Example 13
Source File: ImageRequest.java    From volley with Apache License 2.0 5 votes vote down vote up
@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));
        }
    }
}
 
Example 14
Source File: JsonArrayRequest.java    From volley with Apache License 2.0 5 votes vote down vote up
@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));
    }
}
 
Example 15
Source File: JsonObjectRequest.java    From volley with Apache License 2.0 5 votes vote down vote up
@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));
    }
}
 
Example 16
Source File: FastJsonRequest.java    From Tpay with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Response<BaseMsg> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        return Response.success(
                JSON.parseObject(jsonString, BaseMsg.class), HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 17
Source File: StringRequestGet.java    From Tpay with GNU General Public License v3.0 5 votes vote down vote up
@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 (JSONException je) {
        return Response.error(new ParseError(je));
    }
}
 
Example 18
Source File: ImageRequest.java    From volley with Apache License 2.0 4 votes vote down vote up
/** 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) {
        decodeOptions.inPreferredConfig = mDecodeConfig;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.
        decodeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;

        // Then compute the dimensions we would ideally like to decode to.
        int desiredWidth =
                getResizedDimension(
                        mMaxWidth, mMaxHeight, actualWidth, actualHeight, mScaleType);
        int desiredHeight =
                getResizedDimension(
                        mMaxHeight, mMaxWidth, actualHeight, actualWidth, mScaleType);

        // Decode to the nearest power of two scaling factor.
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
        decodeOptions.inSampleSize =
                findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
        Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);

        // If necessary, scale down to the maximal acceptable size.
        if (tempBitmap != null
                && (tempBitmap.getWidth() > desiredWidth
                        || tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }

    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}
 
Example 19
Source File: ImageRequest.java    From device-database with Apache License 2.0 4 votes vote down vote up
/**
 * 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) {
        decodeOptions.inPreferredConfig = mDecodeConfig;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.
        decodeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;

        // Then compute the dimensions we would ideally like to decode to.
        int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight,
                actualWidth, actualHeight, mScaleType);
        int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth,
                actualHeight, actualWidth, mScaleType);

        // Decode to the nearest power of two scaling factor.
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
        decodeOptions.inSampleSize =
            findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
        Bitmap tempBitmap =
            BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);

        // If necessary, scale down to the maximal acceptable size.
        if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth ||
                tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap,
                    desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }

    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}
 
Example 20
Source File: ImageRequest.java    From SaveVolley with Apache License 2.0 4 votes vote down vote up
/**
 * The real guts of parseNetworkResponse. Broken out for readability.
 */
/*
 * 这里开始真正解析 网络请求结果( 响应 )NetworkResponse
 * NetworkResponse -> Response<Bitmap> 的转换
 */
private Response<Bitmap> doParse(NetworkResponse response) {
    // 拿到结果数据
    byte[] data = response.data;
    // 实例化一个 BitmapFactory.Options 用于解析数据成 Bitmap
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    Bitmap bitmap = null;
    // 如果缺少 最大宽度 和 最大高度
    if (mMaxWidth == 0 && mMaxHeight == 0) {
        // 设置 BitmapFactory.Options.Config
        decodeOptions.inPreferredConfig = mDecodeConfig;
        // 开始生成 Bitmap
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.

        /**
         * 如果存在 最大宽度 和 最大高度
         */

        /*
         * 由于一下四行操作只是想拿到这个 Bitmap 的自身的实际宽高,但又不想申请一个 Bitmap 内存
         * 可以设置 inJustDecodeBounds = true,只是读图片大小,不申请 Bitmap 内存
         * BitmapFactory.decodeByteArray(...) 的时候,就会 return null
         * 此时,再通过 BitmapFactory.Options 内被设置好的 outWidth 和 outHeight
         * 拿到该 Bitmap 的自身的实际宽高
         */
        decodeOptions.inJustDecodeBounds = true;
        /*
         * 这里正常是 Bitmap bitmap = BitmapFactory.decodeByteArray(...)
         * 但是由于上面设置了 inJustDecodeBounds = true
         * 这里一定返回 null
         * 但是这里为 BitmapFactory.Options 设置了 Bitmap 的数据参数
         * 所以下面能拿到 Bitmap 的实际宽高
         */
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        // 记录该 Bitmap 的实际宽度
        int actualWidth = decodeOptions.outWidth;
        // 记录该 Bitmap 的实际高度
        int actualHeight = decodeOptions.outHeight;

        // Then compute the dimensions we would ideally like to decode to.

        // 根据 最宽高、Bitmap 实际宽高 以及 ImageView.ScaleType,计算出 需求宽度
        int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight,
                mScaleType);
        // 根据 最宽高、Bitmap 实际宽高 以及 ImageView.ScaleType,计算出 需求高度
        int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight,
                actualWidth, mScaleType);

        // Decode to the nearest power of two scaling factor.
        // 关闭 inJustDecodeBounds,因为以下要进行真实的 Bitmap 内存申请
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;

        /*
         * 计算缩放比例
         * 如果 BitmapFactory.Options.inSampleSize = 4,那么宽高为 原 Bitmap 的 1/4
         */
        decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth,
                desiredHeight);
        // 解析出 测试 Bitmap
        Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);

        // If necessary, scale down to the maximal acceptable size.

        /*
         * 上面解析出的 测试 Bitmap
         * 如果 测试 Bitmap 的宽高 超过 需求宽高
         * 重新 根据 需求宽高 再拿 测试 Bitmap 解析一遍
         * 得到 最终 Bitmap 返回
         */
        if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth ||
                tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }
    /*
     * 没有解析出的 Bitmap,调用错误回调,回调一个 ParseError
     * 有解析出的 Bitmap,调用 解析结果数据 的回调接口,回调 Bitmap
     */
    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}