List of usage examples for com.squareup.okhttp Call cancel
public void cancel()
From source file:org.quantumbadger.redreader.http.okhttp.OKHTTPBackend.java
License:Open Source License
@Override public Request prepareRequest(final Context context, final RequestDetails details) { final com.squareup.okhttp.Request.Builder builder = new com.squareup.okhttp.Request.Builder(); builder.header("User-Agent", Constants.ua(context)); final List<PostField> postFields = details.getPostFields(); if (postFields != null) { builder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), PostField.encodeList(postFields))); } else {/*from w ww. ja v a2 s. c o m*/ builder.get(); } builder.url(details.getUrl().toString()); builder.cacheControl(CacheControl.FORCE_NETWORK); final AtomicReference<Call> callRef = new AtomicReference<>(); return new Request() { public void executeInThisThread(final Listener listener) { final Call call = mClient.newCall(builder.build()); callRef.set(call); try { final Response response; try { response = call.execute(); } catch (IOException e) { listener.onError(CacheRequest.REQUEST_FAILURE_CONNECTION, e, null); return; } final int status = response.code(); if (status == 200 || status == 202) { final ResponseBody body = response.body(); final InputStream bodyStream; final Long bodyBytes; if (body != null) { bodyStream = body.byteStream(); bodyBytes = body.contentLength(); } else { // TODO error bodyStream = null; bodyBytes = null; } final String contentType = response.header("Content-Type"); listener.onSuccess(contentType, bodyBytes, bodyStream); } else { listener.onError(CacheRequest.REQUEST_FAILURE_REQUEST, null, status); } } catch (Throwable t) { listener.onError(CacheRequest.REQUEST_FAILURE_CONNECTION, t, null); } } @Override public void cancel() { final Call call = callRef.getAndSet(null); if (call != null) { call.cancel(); } } @Override public void addHeader(final String name, final String value) { builder.addHeader(name, value); } }; }
From source file:retrofit.KOkHttpCall.java
License:Apache License
private void exeRequest(final Callback<T> callback, final Request request, final boolean loadnetElseCache) { com.squareup.okhttp.Call rawCall; try {// w w w . jav a2 s .com rawCall = client.newCall(request); } catch (Throwable t) { callback.onFailure(t); return; } if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; rawCall.enqueue(new com.squareup.okhttp.Callback() { private void callFailure(Throwable e) { try { callback.onFailure(e); } catch (Throwable t) { t.printStackTrace(); } } private void callSuccess(Response<T> response) { try { callback.onResponse(response); } catch (Throwable t) { t.printStackTrace(); } } @Override public void onFailure(Request request, IOException e) { callFailure(e); } @Override public void onResponse(com.squareup.okhttp.Response rawResponse) { Response<T> response; try { response = parseResponse(rawResponse, request); } catch (Throwable e) { if (loadnetElseCache) { cacheExecutor.execute(new CallbackRunnable<T>(callback, cacheCallbackExecutor) { @Override public Response<T> obtainResponse() { return execCacheRequest(request); } }); return; } callFailure(e); return; } callSuccess(response); } }); }
From source file:retrofit.KOkHttpCall.java
License:Apache License
public Response<T> execute() throws IOException { synchronized (this) { if (executed) throw new IllegalStateException("Already executed"); executed = true;//from w ww . j a va 2 s. c o m } com.squareup.okhttp.Request request = createRequest(); // ----------------------------------------------------------------------cgp String headerValue = request.header("RequestMode"); if (!TextUtils.isEmpty(headerValue)) { switch (headerValue) { case RequestMode.LOAD_NETWORK_ELSE_CACHE:// ?? com.squareup.okhttp.Call rawCall = client.newCall(request); if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; Response<T> response; try { response = parseResponse(rawCall.execute(), request); } catch (Exception e) { response = execCacheRequest(request); } return response; case RequestMode.LOAD_CACHE_ELSE_NETWORK:// ? // ---------------------? response = execCacheRequest(request); if (response != null) { return response; } // ---------------------? // case RequestMode.LOAD_DEFAULT: case RequestMode.LOAD_NETWORK_ONLY: default: break;// } } // ----------------------------------------------------------------------cgp com.squareup.okhttp.Call rawCall = client.newCall(request); if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; return parseResponse(rawCall.execute(), request); }
From source file:retrofit.OkHttpCall.java
License:Apache License
@Override public void enqueue(final Callback<T> callback) { synchronized (this) { if (executed) throw new IllegalStateException("Already executed"); executed = true;//from ww w.ja v a 2 s .c o m } com.squareup.okhttp.Call rawCall; try { rawCall = createRawCall(); } catch (Throwable t) { callback.onFailure(t); return; } if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; rawCall.enqueue(new com.squareup.okhttp.Callback() { private void callFailure(Throwable e) { try { callback.onFailure(e); } catch (Throwable t) { t.printStackTrace(); } } private void callSuccess(Response<T> response) { try { callback.onResponse(response, retrofit); } catch (Throwable t) { t.printStackTrace(); } } @Override public void onFailure(Request request, IOException e) { callFailure(e); } @Override public void onResponse(com.squareup.okhttp.Response rawResponse) { Response<T> response; try { response = parseResponse(rawResponse); } catch (Throwable e) { callFailure(e); return; } callSuccess(response); } }); }
From source file:retrofit2.OkHttpCall.java
License:Apache License
@Override public void enqueue(final Callback<T> callback) { synchronized (this) { if (executed) throw new IllegalStateException("Already executed."); executed = true;/*from ww w . jav a 2s. co m*/ } com.squareup.okhttp.Call rawCall; try { rawCall = createRawCall(); } catch (Throwable t) { callback.onFailure(t); return; } if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; rawCall.enqueue(new com.squareup.okhttp.Callback() { private void callFailure(Throwable e) { try { callback.onFailure(e); } catch (Throwable t) { t.printStackTrace(); } } private void callSuccess(Response<T> response) { try { callback.onResponse(response); } catch (Throwable t) { t.printStackTrace(); } } @Override public void onFailure(Request request, IOException e) { callFailure(e); } @Override public void onResponse(com.squareup.okhttp.Response rawResponse) { Response<T> response; try { response = parseResponse(rawResponse); } catch (Throwable e) { callFailure(e); return; } callSuccess(response); } }); }
From source file:retrofit2.OkHttpCall.java
License:Apache License
@Override public Response<T> execute() throws IOException { synchronized (this) { if (executed) throw new IllegalStateException("Already executed."); executed = true;/*www .ja v a 2 s . c om*/ } com.squareup.okhttp.Call rawCall = createRawCall(); if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; return parseResponse(rawCall.execute()); }