Java Code Examples for com.lzy.okgo.model.Response#success()

The following examples show how to use com.lzy.okgo.model.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: DefaultCachePolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onAnalysisResponse(final Call call, final okhttp3.Response response) {
    if (response.code() != 304) return false;

    if (cacheEntity == null) {
        final Response<T> error = Response.error(true, call, response, CacheException.NON_AND_304(request.getCacheKey()));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onError(error);
                mCallback.onFinish();
            }
        });
    } else {
        final Response<T> success = Response.success(true, cacheEntity.getData(), call, response);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onCacheSuccess(success);
                mCallback.onFinish();
            }
        });
    }
    return true;
}
 
Example 2
Source File: DefaultCachePolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    //HTTP cache protocol
    if (response.isSuccessful() && response.code() == 304) {
        if (cacheEntity == null) {
            response = Response.error(true, rawCall, response.getRawResponse(), CacheException.NON_AND_304(request.getCacheKey()));
        } else {
            response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
        }
    }
    return response;
}
 
Example 3
Source File: FirstCacheRequestPolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    //同步请求,不能返回两次,只返回正确的数据
    Response<T> response;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example 4
Source File: NoneCacheRequestPolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = null;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    if (response == null) {
        response = requestNetworkSync();
    }
    return response;
}
 
Example 5
Source File: RequestFailedCachePolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void onError(final Response<T> error) {

    if (cacheEntity != null) {
        final Response<T> cacheSuccess = Response.success(true, cacheEntity.getData(), error.getRawCall(), error.getRawResponse());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onCacheSuccess(cacheSuccess);
                mCallback.onFinish();
            }
        });
    } else {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCallback.onError(error);
                mCallback.onFinish();
            }
        });
    }
}
 
Example 6
Source File: DefaultCachePolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    //HTTP cache protocol
    if (response.isSuccessful() && response.code() == 304) {
        if (cacheEntity == null) {
            response = Response.error(true, rawCall, response.getRawResponse(), CacheException.NON_AND_304(request.getCacheKey()));
        } else {
            response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
        }
    }
    return response;
}
 
Example 7
Source File: FirstCacheRequestPolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    //同步请求,不能返回两次,只返回正确的数据
    Response<T> response;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example 8
Source File: NoneCacheRequestPolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = null;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    if (response == null) {
        response = requestNetworkSync();
    }
    return response;
}
 
Example 9
Source File: BaseCachePolicy.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
protected Response<T> requestNetworkSync() {
    try {
        okhttp3.Response response = rawCall.execute();
        int responseCode = response.code();

        //network error
        if (responseCode == 404 || responseCode >= 500) {
            return Response.error(false, rawCall, response, HttpException.NET_ERROR());
        }

        T body = request.getConverter().convertResponse(response);
        //save cache when request is successful
        saveCache(response.headers(), body);
        return Response.success(false, body, rawCall, response);
    } catch (Throwable throwable) {
        if (throwable instanceof SocketTimeoutException && currentRetryCount < request.getRetryCount()) {
            currentRetryCount++;
            rawCall = request.getRawCall();
            if (canceled) {
                rawCall.cancel();
            } else {
                requestNetworkSync();
            }
        }
        return Response.error(false, rawCall, null, throwable);
    }
}
 
Example 10
Source File: RequestFailedCachePolicy.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example 11
Source File: BaseCachePolicy.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/** 同步的方式请求网络 **/
    protected Response<T> requestNetworkSync() {
        try {
            okhttp3.Response response = rawCall.execute();
            int responseCode = response.code();

            //network error
            if (responseCode == 404 || responseCode >= 500) {
                return Response.error(false, rawCall, response, HttpException.NET_ERROR());
            }
            Converter<T> respConverter = request.getConverter();
            T body = null;
            if (respConverter != null) {
                body = respConverter.convertResponse(response);
            }
            else {
                OkLogger.e(TAG, "-->requestNetworkSync() respConverter is null...");
            }
//            T body = request.getConverter().convertResponse(response);
            //save cache when request is successful
            saveCache(response.headers(), body);
            return Response.success(false, body, rawCall, response);
        } catch (Throwable throwable) {
            if (throwable instanceof SocketTimeoutException && currentRetryCount < request.getRetryCount()) {
                currentRetryCount++;
                rawCall = request.getRawCall();
                if (canceled) {
                    rawCall.cancel();
                } else {
                    requestNetworkSync();
                }
            }
            return Response.error(false, rawCall, null, throwable);
        }
    }
 
Example 12
Source File: RequestFailedCachePolicy.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example 13
Source File: DefaultCachePolicy.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean onAnalysisResponse(final Call call, final okhttp3.Response response) {
        if (response.code() != 304) return false;

        if (cacheEntity == null) {
            final Response<T> error = Response.error(true, call, response, CacheException.NON_AND_304(request.getCacheKey()));
            if (mCallback != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callbackOnError(error);
                        callbackOnFinish();
//                        mCallback.onError(error);
//                        mCallback.onFinish();
                    }
                });
            }
//            runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    mCallback.onError(error);
//                    mCallback.onFinish();
//                }
//            });
        } else {
            final Response<T> success = Response.success(true, cacheEntity.getData(), call, response);
            if (mCallback != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callbackOnCacheSuccess(success);
                        callbackOnFinish();
//                        mCallback.onCacheSuccess(success);
//                        mCallback.onFinish();
                    }
                });
            }
//            runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    mCallback.onCacheSuccess(success);
//                    mCallback.onFinish();
//                }
//            });
        }
        return true;
    }
 
Example 14
Source File: RequestFailedCachePolicy.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
@Override
    public void onError(final Response<T> error) {

        if (cacheEntity != null) {
            final Response<T> cacheSuccess = Response.success(true, cacheEntity.getData(), error.getRawCall(), error.getRawResponse());
            if (mCallback != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callbackOnCacheSuccess(cacheSuccess);
                        callbackOnFinish();
//                        mCallback.onCacheSuccess(cacheSuccess);
//                        mCallback.onFinish();
                    }
                });
            }
//            runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    mCallback.onCacheSuccess(cacheSuccess);
//                    mCallback.onFinish();
//                }
//            });
        } else {
            if (mCallback != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callbackOnError(error);
                        callbackOnFinish();
//                        mCallback.onError(error);
//                        mCallback.onFinish();
                    }
                });
            }
//            runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    mCallback.onError(error);
//                    mCallback.onFinish();
//                }
//            });
        }
    }