public OkHttpListenableFuture(Call call) { this.call = call; this.call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { set(new OkHttp3ClientHttpResponse(response)); } @Override public void onFailure(Call call, IOException ex) { setException(ex); } }); }
public OkHttpListenableFuture(Call call) { this.call = call; this.call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { set(new OkHttp3ClientHttpResponse(response)); } @Override public void onFailure(Call call, IOException ex) { setException(ex); } }); }
@Override public void setCallback(Callback<T> callback) { this.callback = callback; call.enqueue(this); }
public void run() throws Exception { Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody responseBody = response.body()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); for (int i = 0, size = responseHeaders.size(); i < size; i++) { System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); } System.out.println(responseBody.string()); } } }); }
private void executeRequests(final String hostname, List<Call> networkRequests, final List<InetAddress> responses, final List<Exception> failures) { final CountDownLatch latch = new CountDownLatch(networkRequests.size()); for (Call call : networkRequests) { call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { synchronized (failures) { failures.add(e); } latch.countDown(); } @Override public void onResponse(Call call, Response response) { processResponse(response, hostname, responses, failures); latch.countDown(); } }); } try { latch.await(); } catch (InterruptedException e) { failures.add(e); } }
public void run() throws Exception { Request washingtonPostRequest = new Request.Builder() .url("https://www.washingtonpost.com/") .build(); client.newCall(washingtonPostRequest).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody body = response.body()) { // Consume and discard the response body. body.source().readByteString(); } } }); Request newYorkTimesRequest = new Request.Builder() .url("https://www.nytimes.com/") .build(); client.newCall(newYorkTimesRequest).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody body = response.body()) { // Consume and discard the response body. body.source().readByteString(); } } }); }
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); Call post(String url, String json, Callback callback) { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Call call = client.newCall(request); call.enqueue(callback); return call; }
public void connect(OkHttpClient client) { client = client.newBuilder() .eventListener(EventListener.NONE) .build(); call = client.newCall(request); call.timeout().clearTimeout(); call.enqueue(this); }
@Override public void connect() throws IOException { if (executed) return; Call call = buildCall(); executed = true; call.enqueue(this); synchronized (lock) { try { while (connectPending && response == null && callFailure == null) { lock.wait(); // Wait 'til the network interceptor is reached or the call fails. } if (callFailure != null) { throw propagate(callFailure); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); // Retain interrupted status. throw new InterruptedIOException(); } } }
@Override public void loadData(@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) { Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl()); for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { String key = headerEntry.getKey(); requestBuilder.addHeader(key, headerEntry.getValue()); } Request request = requestBuilder.build(); this.callback = callback; call = client.newCall(request); call.enqueue(this); }
public static void sendOkHttpRequest(String address, okhttp3.Callback callback) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(address).build(); client.newCall(request).enqueue(callback); }
public void send(DatadogHttpReporter.DatadogHttpRequest request) throws Exception { String postBody = serialize(request.getSeries()); Request r = new Request.Builder() .url(seriesUrl) .post(RequestBody.create(MEDIA_TYPE, postBody)) .build(); client.newCall(r).enqueue(EmptyCallback.getEmptyCallback()); }
@Override public void loadData(@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) { Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl()); for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { String key = headerEntry.getKey(); requestBuilder.addHeader(key, headerEntry.getValue()); } Request request = requestBuilder.build(); this.callback = callback; call = client.newCall(request); call.enqueue(this); }
call = Internal.instance.newWebSocketCall(client, request); call.timeout().clearTimeout(); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
private void httpDelete(URI uri) { Request request = prepareRequest(HttpUrl.get(uri)) .delete() .build(); httpClient.newCall(request).enqueue(new NullCallback()); }
call.enqueue(new okhttp3.Callback() { @Override public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse) { Response<T> response;
@Test public void testVoteNoMatchingAccount() { Preferences.setUsername(RuntimeEnvironment.application, "another"); UserServices.Callback callback = mock(UserServices.Callback.class); assertFalse(userServices.voteUp(RuntimeEnvironment.application, "1", callback)); verify(call, never()).enqueue(any(Callback.class)); }
@Test public void testCommentNotLoggedIn() { Preferences.setUsername(RuntimeEnvironment.application, null); UserServices.Callback callback = mock(UserServices.Callback.class); userServices.reply(RuntimeEnvironment.application, "1", "reply", callback); verify(call, never()).enqueue(any(Callback.class)); verify(callback).onDone(eq(false)); }
@Test public void testVoteNoAccount() { ShadowAccountManager.get(RuntimeEnvironment.application) .removeAccount(account, null, null); UserServices.Callback callback = mock(UserServices.Callback.class); assertFalse(userServices.voteUp(RuntimeEnvironment.application, "1", callback)); verify(call, never()).enqueue(any(Callback.class)); }
@Override public void onFailure(okhttp3.Call call, IOException e) { if (e instanceof SocketTimeoutException && currentRetryCount < request.getRetryCount()) { //retry when timeout currentRetryCount++; rawCall = request.getRawCall(); if (canceled) { rawCall.cancel(); } else { rawCall.enqueue(this); } } else { if (!call.isCanceled()) { Response<T> error = Response.error(false, call, null, e); onError(error); } } }