com.android.volley.ServerError Java Examples

The following examples show how to use com.android.volley.ServerError. 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: RequestSingletonFactory.java    From Netease with GNU General Public License v3.0 7 votes vote down vote up
@Override
public void onErrorResponse(VolleyError error) {
    error.printStackTrace();
    Log.d("RVA", "error:" + error);

    int errorCode = 0;
    if (error instanceof TimeoutError) {
        errorCode = -7;
    } else if (error instanceof NoConnectionError) {
        errorCode = -1;
    } else if (error instanceof AuthFailureError) {
        errorCode = -6;
    } else if (error instanceof ServerError) {
        errorCode = 0;
    } else if (error instanceof NetworkError) {
        errorCode = -1;
    } else if (error instanceof ParseError) {
        errorCode = -8;
    }
    Toast.makeText(contextHold, ErrorCode.errorCodeMap.get(errorCode), Toast.LENGTH_SHORT).show();
}
 
Example #2
Source File: BasicNetworkTest.java    From volley with Apache License 2.0 6 votes vote down vote up
@Test
public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        HttpResponse fakeResponse = new HttpResponse(i, Collections.<Header>emptyList());
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #3
Source File: RequestFragment.java    From OkVolley with Apache License 2.0 6 votes vote down vote up
private void post() {

        BaseRequest request = new BaseRequest(Request.Method.POST, "http://192.168.2.19:5000/test1",
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Toast.makeText(getActivity(), jsonObject.toString(), Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (error instanceof ServerError) {
                            Toast.makeText(getActivity(),
                                    new String(((ServerError) error).networkResponse.data, Charset.defaultCharset()), Toast.LENGTH_SHORT)
                                    .show();
                        } else {
                            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
        );
//        request.form("text", "test " + SystemClock.elapsedRealtime());
        request.setTag("request");
        OkVolley.getInstance().getRequestQueue().add(request);
    }
 
Example #4
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse =
                new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork =
                new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #5
Source File: BasicNetworkTest.java    From product-emm with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse =
                new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork =
                new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #6
Source File: SyncAdapter.java    From attendee-checkin with Apache License 2.0 6 votes vote down vote up
private long postCheckIn(String attendeeId, String eventId, boolean revert, String cookie) {
    RequestQueue queue = GutenbergApplication.from(getContext()).getRequestQueue();
    RequestFuture<JSONObject> future = RequestFuture.newFuture();
    queue.add(new CheckInRequest(cookie, eventId, attendeeId, revert, future, future));
    try {
        JSONObject object = future.get();
        return object.getLong("checkinTime");
    } catch (InterruptedException | ExecutionException | JSONException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ServerError) {
            ServerError error = (ServerError) cause;
            Log.e(TAG, "Server error: " + new String(error.networkResponse.data));
        }
        Log.e(TAG, "Cannot sync checkin.", e);
    }
    return -1;
}
 
Example #7
Source File: BasicNetworkTest.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
@Test public void serverError_enableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse = new BasicHttpResponse(
                new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack, new ByteArrayPool(4096));
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        request.setShouldRetryServerErrors(true);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
            // expected
        }
        // should retry all 500 errors
        verify(mMockRetryPolicy).retry(any(ServerError.class));
        reset(mMockRetryPolicy);
    }
}
 
Example #8
Source File: BasicNetwork.java    From volley_demo with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #9
Source File: ListIDProfiles.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {

    final NetworkResponse response = error.networkResponse;

    if (response != null && error instanceof ServerError) {

        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
 
Example #10
Source File: BasicNetwork.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the contents of HttpEntity into a byte[].
 */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #11
Source File: MockNetwork.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    if (mNumExceptionsToThrow > 0 || mNumExceptionsToThrow == ALWAYS_THROW_EXCEPTIONS) {
        if (mNumExceptionsToThrow != ALWAYS_THROW_EXCEPTIONS) {
            mNumExceptionsToThrow--;
        }
        throw new ServerError();
    }

    requestHandled = request;
    return new NetworkResponse(mDataToReturn);
}
 
Example #12
Source File: BasicNetwork.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #13
Source File: MockNetwork.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    if (mNumExceptionsToThrow > 0 || mNumExceptionsToThrow == ALWAYS_THROW_EXCEPTIONS) {
        if (mNumExceptionsToThrow != ALWAYS_THROW_EXCEPTIONS) {
            mNumExceptionsToThrow--;
        }
        throw new ServerError();
    }

    requestHandled = request;
    return new NetworkResponse(mDataToReturn);
}
 
Example #14
Source File: BVStartRequest.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {

    final NetworkResponse response = error.networkResponse;

    if (response != null && error instanceof ServerError) {

        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
 
Example #15
Source File: BasicNetwork.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #16
Source File: MockNetwork.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    if (mNumExceptionsToThrow > 0 || mNumExceptionsToThrow == ALWAYS_THROW_EXCEPTIONS) {
        if (mNumExceptionsToThrow != ALWAYS_THROW_EXCEPTIONS) {
            mNumExceptionsToThrow--;
        }
        throw new ServerError();
    }

    requestHandled = request;
    return new NetworkResponse(mDataToReturn);
}
 
Example #17
Source File: BVAuthRequest.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {

    final NetworkResponse response = error.networkResponse;

    if (response != null && error instanceof ServerError) {

        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
 
Example #18
Source File: ContentDownloadService.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private void onRequestError(VolleyError error, @NonNull ImageFile img, @NonNull DocumentFile dir, @NonNull String backupUrl) {
    // Try with the backup URL, if it exists and if the current image isn't a backup itself
    if (!img.isBackup() && !backupUrl.isEmpty()) {
        tryUsingBackupUrl(img, dir, backupUrl);
        return;
    }

    // If no backup, then process the error
    String statusCode = (error.networkResponse != null) ? error.networkResponse.statusCode + "" : "N/A";
    String message = error.getMessage() + (img.isBackup() ? " (from backup URL)" : "");
    String cause = "";

    if (error instanceof TimeoutError) {
        cause = "Timeout";
    } else if (error instanceof NoConnectionError) {
        cause = "No connection";
    } else if (error instanceof AuthFailureError) { // 403's fall in this category
        cause = "Auth failure";
    } else if (error instanceof ServerError) { // 404's fall in this category
        cause = "Server error";
    } else if (error instanceof NetworkError) {
        cause = "Network error";
    } else if (error instanceof ParseError) {
        cause = "Network parse error";
    }

    Timber.w(error);

    updateImageStatusUri(img, false, "");
    logErrorRecord(img.content.getTargetId(), ErrorType.NETWORKING, img.getUrl(), img.getName(), cause + "; HTTP statusCode=" + statusCode + "; message=" + message);
}
 
Example #19
Source File: BasicNetwork.java    From volley with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of an InputStream into a byte[]. */
private byte[] inputStreamToBytes(InputStream in, int contentLength)
        throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, contentLength);
    byte[] buffer = null;
    try {
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            // This can happen if there was an exception above that left the stream in
            // an invalid state.
            VolleyLog.v("Error occurred when closing InputStream");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #20
Source File: BasicNetwork.java    From jus with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #21
Source File: BasicNetwork.java    From okulus with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #22
Source File: BasicNetwork.java    From FeedListViewDemo with MIT License 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #23
Source File: BasicNetwork.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #24
Source File: BasicNetwork.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #25
Source File: BasicNetwork.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes =
            new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
 
Example #26
Source File: MockNetwork.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    if (mNumExceptionsToThrow > 0 || mNumExceptionsToThrow == ALWAYS_THROW_EXCEPTIONS) {
        if (mNumExceptionsToThrow != ALWAYS_THROW_EXCEPTIONS) {
            mNumExceptionsToThrow--;
        }
        throw new ServerError();
    }

    requestHandled = request;
    return new NetworkResponse(mDataToReturn);
}
 
Example #27
Source File: JacksonNetwork.java    From volley-jackson-extension with Apache License 2.0 5 votes vote down vote up
/**
 * Copied from {@link com.android.volley.toolbox.BasicNetwork}
 *
 * Reads the contents of HttpEntity into a byte[].
 *
 */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
	PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
	byte[] buffer = null;
	try {
		InputStream in = entity.getContent();
		if (in == null) {
			throw new ServerError();
		}
		buffer = mPool.getBuf(1024);
		int count;
		while ((count = in.read(buffer)) != -1) {
			bytes.write(buffer, 0, count);
		}
		return bytes.toByteArray();
	} finally {
		try {
			// Close the InputStream and release the resources by "consuming the content".
			entity.consumeContent();
		} catch (IOException e) {
			// This can happen if there was an exception above that left the entity in
			// an invalid state.
			VolleyLog.v("Error occured when calling consumingContent");
		}
		mPool.returnBuf(buffer);
		bytes.close();
	}
}
 
Example #28
Source File: MockNetwork.java    From device-database with Apache License 2.0 5 votes vote down vote up
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    if (mNumExceptionsToThrow > 0 || mNumExceptionsToThrow == ALWAYS_THROW_EXCEPTIONS) {
        if (mNumExceptionsToThrow != ALWAYS_THROW_EXCEPTIONS) {
            mNumExceptionsToThrow--;
        }
        throw new ServerError();
    }

    requestHandled = request;
    return new NetworkResponse(mDataToReturn);
}
 
Example #29
Source File: FetchIDProfile.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {

    final NetworkResponse response = error.networkResponse;

    if (response != null && error instanceof ServerError) {

        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
 
Example #30
Source File: CreateIDProfile.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Used for debugging only to view verbose error information
 *
 * @param error the {@link VolleyError}
 */
private void verboseError(@NonNull final VolleyError error) {

    final NetworkResponse response = error.networkResponse;

    if (response != null && error instanceof ServerError) {

        try {
            final String result = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            MyLog.i(CLS_NAME, "result: " + result);
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}