List of usage examples for com.squareup.okhttp Response body
ResponseBody body
To view the source code for com.squareup.okhttp Response body.
Click Source Link
From source file:com.anony.okhttp.sample.AsynchronousGet.java
License:Apache License
public void run() throws Exception { Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build(); client.newCall(request).enqueue(new Callback() { @Override//from ww w . ja va2 s .co m public void onFailure(Request request, IOException e) { e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { 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(response.body().string()); } }); }
From source file:com.anony.okhttp.sample.Authenticate.java
License:Apache License
public void run() throws Exception { client.setAuthenticator(new Authenticator() { @Override//from w w w .j av a 2s .c o m public Request authenticate(Proxy proxy, Response response) { System.out.println("Authenticating for response: " + response); System.out.println("Challenges: " + response.challenges()); String credential = Credentials.basic("jesse", "password1"); return response.request().newBuilder().header("Authorization", credential).build(); } @Override public Request authenticateProxy(Proxy proxy, Response response) { return null; // Null indicates no attempt to authenticate. } }); Request request = new Request.Builder().url("http://publicobject.com/secrets/hellosecret.txt").build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
From source file:com.anony.okhttp.sample.CacheResponse.java
License:Apache License
public void run() throws Exception { Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build(); Response response1 = client.newCall(request).execute(); if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1); String response1Body = response1.body().string(); System.out.println("Response 1 response: " + response1); System.out.println("Response 1 cache response: " + response1.cacheResponse()); System.out.println("Response 1 network response: " + response1.networkResponse()); Response response2 = client.newCall(request).execute(); if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2); String response2Body = response2.body().string(); System.out.println("Response 2 response: " + response2); System.out.println("Response 2 cache response: " + response2.cacheResponse()); System.out.println("Response 2 network response: " + response2.networkResponse()); System.out.println("Response 2 equals Response 1? " + response1Body.equals(response2Body)); }
From source file:com.anony.okhttp.sample.CheckHandshake.java
License:Apache License
public void run() throws Exception { Request request = new Request.Builder().url("https://publicobject.com/helloworld.txt").build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
From source file:com.anony.okhttp.sample.CustomTrust.java
License:Apache License
public void run() throws Exception { Request request = new Request.Builder().url("https://publicobject.com/helloworld.txt").build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); for (int i = 0; i < responseHeaders.size(); i++) { System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); }/*from ww w . ja va 2 s .c o m*/ System.out.println(response.body().string()); }
From source file:com.anony.okhttp.sample.LoggingInterceptors.java
License:Apache License
public void run() throws Exception { Request request = new Request.Builder().url("https://publicobject.com/helloworld.txt").build(); Response response = client.newCall(request).execute(); response.body().close(); }
From source file:com.anony.okhttp.sample.ParseResponseWithGson.java
License:Apache License
public void run() throws Exception { Request request = new Request.Builder().url("https://api.github.com/gists/c2a7c39532239ff261be").build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Gist gist = gson.fromJson(response.body().charStream(), Gist.class); for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) { System.out.println(entry.getKey()); System.out.println(entry.getValue().content); }//from www . j a va 2 s . c om }
From source file:com.anony.okhttp.sample.PostFile.java
License:Apache License
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(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
From source file:com.anony.okhttp.sample.PostForm.java
License:Apache License
public void run() throws Exception { RequestBody formBody = new FormEncodingBuilder().add("search", "Jurassic Park").build(); Request request = new Request.Builder().url("https://en.wikipedia.org/w/index.php").post(formBody).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
From source file:com.anony.okhttp.sample.PostMultipart.java
License:Apache License
public void run() throws Exception { // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart("title", "Square Logo").addFormDataPart("image", null, RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png"))) .build();// w w w . j av a2 s . c om Request request = new Request.Builder().header("Authorization", "Client-ID " + IMGUR_CLIENT_ID) .url("https://api.imgur.com/3/image").post(requestBody).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }