public void run() throws Exception { Request request = new Request.Builder() .url("https://publicobject.com/helloworld.txt") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
public void run() throws Exception { Request request = new Request.Builder() .url("https://api.github.com/repos/square/okhttp/issues") .header("User-Agent", "OkHttp Headers.java") .addHeader("Accept", "application/json; q=0.5") .addHeader("Accept", "application/vnd.github.v3+json") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println("Server: " + response.header("Server")); System.out.println("Date: " + response.header("Date")); System.out.println("Vary: " + response.headers("Vary")); } }
@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()); } } });
Subscription subscription = Observable.create(new Observable.OnSubscribe<Response>() { OkHttpClient client = new OkHttpClient(); @Override public void call(Subscriber<? super Response> subscriber) { try { Response response = client.newCall(new Request.Builder().url("your url").build()).execute(); subscriber.onNext(response); subscriber.onCompleted(); if (!response.isSuccessful()) subscriber.onError(new Exception("error")); } catch (IOException e) { subscriber.onError(e); } } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<Response>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Response response) { } });
public void run() throws Exception { Request request = new Request.Builder() .url("https://publicobject.com/secrets/hellosecret.txt") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
post("http://www.roundsapp.com/post", json, new Callback() { @Override public void onFailure(Request request, Throwable throwable) { // Something went wrong } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { String responseStr = response.body().string(); // Do what you want to do with the response. } else { // Request not successful } } });
private void validateApiKey() { Request r = new Request.Builder().url(validateUrl).get().build(); try (Response response = client.newCall(r).execute()) { if (!response.isSuccessful()) { throw new IllegalArgumentException( String.format("API key: %s is invalid", apiKey)); } } catch (IOException e) { throw new IllegalStateException("Failed contacting Datadog to validate API key", e); } }
.build(); Request request = new Request.Builder() .url(serverURL) .post(requestBody) if (!response.isSuccessful()) {
public void run() throws Exception { Request request = new Request.Builder() .url("http://publicobject.com/secrets/hellosecret.txt") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
post("http://www.roundsapp.com/post", "", new Callback() { @Override public void onFailure(Call call, IOException e) { // Something went wrong } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String responseStr = response.body().string(); // Do what you want to do with the response. } else { // Request not successful } } });
public void run() throws Exception { Request request = new Request.Builder() .url("https://publicobject.com/robots.txt") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); for (Certificate certificate : response.handshake().peerCertificates()) { System.out.println(CertificatePinner.pin(certificate)); } } }
public void run() throws Exception { File file = new File("README.md"); Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file)) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
@Override public void onResponse(@NonNull Call call, @NonNull Response response) { responseBody = response.body(); if (response.isSuccessful()) { long contentLength = Preconditions.checkNotNull(responseBody).contentLength(); stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); callback.onDataReady(stream); } else { callback.onLoadFailed(new HttpException(response.message(), response.code())); } }
public void run() throws Exception { for (int i = 0; i < 5; i++) { System.out.println(" Request: " + i); Request request = new Request.Builder() .url("https://api.github.com/search/repositories?q=http") .build(); OkHttpClient clientForCall; if (i == 2) { // Force this request's response to be written to the cache. This way, subsequent responses // can be read from the cache. System.out.println("Force cache: true"); clientForCall = client.newBuilder() .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .build(); } else { System.out.println("Force cache: false"); clientForCall = client; } try (Response response = clientForCall.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(" Network: " + (response.networkResponse() != null)); System.out.println(); } } }
public void run() throws Exception { Request request = new Request.Builder() .url("https://api.github.com/gists/c2a7c39532239ff261be") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Gist gist = gistJsonAdapter.fromJson(response.body().source()); for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) { System.out.println(entry.getKey()); System.out.println(entry.getValue().content); } } }
@Override public void onResponse(Call call, Response response) throws IOException { try (ResponseBody responseBody = response.body()) { if (!response.isSuccessful()) { log.warn("Failed to download image " + item.getAvatar()); return; } BufferedImage icon; synchronized (ImageIO.class) { icon = ImageIO.read(responseBody.byteStream()); } avatar.setIcon(new ImageIcon(icon)); } } });
@Test public void currentSpanVisibleToOtherFilters() throws Exception { delegate = userFilter; String path = "/foo"; Request request = new Request.Builder().url(url(path)) .header(EXTRA_KEY, "abcdefg").build(); try (Response response = client.newCall(request).execute()) { assertThat(response.isSuccessful()).isTrue(); assertThat(response.header(EXTRA_KEY)) .isEqualTo("abcdefg"); } takeSpan(); }
public void run() throws Exception { Request request = new Request.Builder() .url("https://publicobject.com/helloworld.txt") .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.handshake().cipherSuite()); System.out.println(response.body().string()); } }
/** * The /extra endpoint should copy the key {@link #EXTRA_KEY} to the response body using * {@link ExtraFieldPropagation#get(String)}. */ void readsExtra(Request.Builder builder) throws Exception { Request request = builder.url(url("/extra")) // this is the pre-configured key we can pass through .header(EXTRA_KEY, "joey").build(); Response response = get(request); assertThat(response.isSuccessful()).isTrue(); // if we can read the response header, the server must have been able to copy it assertThat(response.body().source().readUtf8()) .isEqualTo("joey"); }
@Test public void traceContextVisibleToOtherFilters() throws Exception { delegate = traceContextFilter; String path = "/foo"; Request request = new Request.Builder().url(url(path)) .header(EXTRA_KEY, "abcdefg").build(); try (Response response = client.newCall(request).execute()) { assertThat(response.isSuccessful()).isTrue(); assertThat(response.header(EXTRA_KEY)) .isEqualTo("abcdefg"); } takeSpan(); }