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

The following examples show how to use com.android.volley.Response#success() . 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: StringRequest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    String parsed;
    try {
        /*
         * 1. HttpHeaderParser 解析编码集
         * 2. 将请求结果 Response 的数据 ( data )通过编码集实例化一个数据 ( data ) String 类型
         */
        parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
    } catch (UnsupportedEncodingException e) {
        // 出现编码异常,就不进行编码处理,直接实例化一个 String
        parsed = new String(response.data);
    }
    /*
     * 解析成功,没有异常
     * 1. 将保存好 通过编码解析的 String 数据 返回
     * 2. 再通过 HttpHeaderParser 从网络请求回来的请求结果 NetworkResponse 的 Header 中提取出一个用于缓存的 Cache.Entry
     */
    return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 2
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 3
Source File: VolleyMultipartRequest.java    From indigenous-android with GNU General Public License v3.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 4
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 5
Source File: PutRequest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 6
Source File: PatchRequest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 7
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 8
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 9
Source File: DeleteRequest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 10
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 11
Source File: StringRequest.java    From volley with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("DefaultCharset")
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    String parsed;
    try {
        parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
    } catch (UnsupportedEncodingException e) {
        // Since minSdkVersion = 8, we can't call
        // new String(response.data, Charset.defaultCharset())
        // So suppress the warning instead.
        parsed = new String(response.data);
    }
    return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 12
Source File: StringRequest.java    From device-database with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 13
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 14
Source File: DeleteRequest.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
@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));
}
 
Example 15
Source File: RestVolleyRequest.java    From RestVolley with Apache License 2.0 4 votes vote down vote up
@Override
protected Response<RestVolleyResponse<T>> parseNetworkResponse(NetworkResponse response) {
    return Response.success(parseNetworkResponse2RVResponse(response), HttpHeaderParser.parseCacheHeaders(response));
}
 
Example 16
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 17
Source File: MockRequest.java    From SaveVolley with Apache License 2.0 4 votes vote down vote up
@Override protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
    parseResponse_called = true;
    return Response.success(response.data, CacheTestUtils.makeRandomCacheEntry(response.data));
}
 
Example 18
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));
    }
}
 
Example 19
Source File: MockRequest.java    From volley with Apache License 2.0 4 votes vote down vote up
@Override
protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
    parseResponse_called = true;
    return Response.success(response.data, CacheTestUtils.makeRandomCacheEntry(response.data));
}
 
Example 20
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));
    }
}