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.frostwire.util.http.OKHTTPClient.java
License:Open Source License
private String getPostSyncResponse(Request.Builder builder) throws IOException { String result = null;// www . j a v a 2 s .c o m final OkHttpClient okHttpClient = newOkHttpClient(); final Response response = this.getSyncResponse(okHttpClient, builder); int httpResponseCode = response.code(); if ((httpResponseCode != HttpURLConnection.HTTP_OK) && (httpResponseCode != HttpURLConnection.HTTP_PARTIAL)) { throw new ResponseCodeNotSupportedException(httpResponseCode); } if (canceled) { onCancel(); } else { result = response.body().string(); onComplete(); } return result; }
From source file:com.gdkm.sfk.utils.coreProgress.ProgressHelper.java
License:Apache License
/** * OkHttpClient/*w w w . ja v a 2s . c om*/ * @param client OkHttpClient * @param progressListener ? * @return ?OkHttpClientclone */ public static OkHttpClient addProgressResponseListener(OkHttpClient client, final ProgressResponseListener progressListener) { // OkHttpClient clone = client.clone(); // clone.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { // Response originalResponse = chain.proceed(chain.request()); //? return originalResponse.newBuilder() .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build(); } }); return clone; }
From source file:com.github.airk.tool.sobitmap.NetworkHunter.java
License:Apache License
@Override File preCacheFile() {//from ww w .j a v a 2 s.com File file = null; try { Request netReq = new Request.Builder().url(request.source.toString()).build(); Response response = client.newCall(netReq).execute(); if (response.code() >= 300) { if (SoBitmap.LOG) { Log.d(SoBitmap.TAG, tag() + ": Network error occurred..." + response.code()); } response.body().close(); request.e = new HuntException(HuntException.REASON_NETWORK_ERROR); request.e.setExtra(response.code() + ""); return null; } if (SoBitmap.LOG) { Log.d(SoBitmap.TAG, tag() + ": Downloading..."); } String size = response.header("content-length"); if (size != null && !request.options.onlyLevel && Long.parseLong(size) > request.options.maxInput) { request.e = new HuntException(HuntException.REASON_TOO_LARGE); return null; } file = new File(request.cacheDir, request.tag); Util.inputStreamToFile(file, response.body().byteStream()); if (SoBitmap.LOG) { Log.d(SoBitmap.TAG, tag() + ": Downloaded to file -> " + file.getAbsolutePath()); } if (!file.exists()) { //UNLIKELY request.e = new HuntException(HuntException.REASON_FILE_NOT_FOUND); request.e.setExtra("Download success but file not found."); return null; } else { if (!request.options.onlyLevel && file.length() / 1024 > request.options.maxInput) { request.e = new HuntException(HuntException.REASON_TOO_LARGE); return null; } } } catch (SocketException e) { e.printStackTrace(); request.e = new HuntException(HuntException.REASON_NETWORK_ERROR); } catch (IOException ignore) { request.e = new HuntException(HuntException.REASON_IO_EXCEPTION); } return file; }
From source file:com.github.arven.rest.AccountServiceTests.java
@Test public void registerAccount() throws Exception { server = new MockWebServer(); server.enqueue(new MockResponse().setHeader("Content-Type", JSON) .setBody(map.writeValueAsString(new ServiceResponse(ServiceResponse.Type.SUCCESS, 200)))); server.enqueue(new MockResponse().setHeader("Content-Type", JSON) .setBody(map.writeValueAsString(new AccountInformation("trfields", "T. R. Fields")))); server.play();//from w ww.j a v a 2 s . c om URL url = server.getUrl("/users/trfields"); RequestBody body = RequestBody.create(JSON, map.writeValueAsString(new AccountInformation("trfields", "T. R. Fields"))); Request request1 = new Request.Builder().url(url).put(body).build(); Response response1 = client.newCall(request1).execute(); Request request2 = new Request.Builder().url(url).get().build(); Response response2 = client.newCall(request2).execute(); RecordedRequest recorded1 = server.takeRequest(); Assert.assertEquals(map.readTree(recorded1.getUtf8Body()), map.readTree(response2.body().string())); Assert.assertEquals(recorded1.getPath(), "/users/trfields"); Assert.assertEquals(recorded1.getMethod(), "PUT"); RecordedRequest recorded2 = server.takeRequest(); Assert.assertEquals(recorded2.getPath(), "/users/trfields"); Assert.assertEquals(recorded2.getMethod(), "GET"); server.shutdown(); }
From source file:com.github.arven.rest.AccountServiceTests.java
@Test public void deleteAccount() throws Exception { server = new MockWebServer(); server.enqueue(new MockResponse().setHeader("Content-Type", JSON) .setBody(map.writeValueAsString(new ServiceResponse(ServiceResponse.Type.SUCCESS, 200)))); server.enqueue(new MockResponse().setHeader("Content-Type", JSON) .setBody(map.writeValueAsString(new ServiceResponse(ServiceResponse.Type.ERROR, 404))) .setStatus("HTTP/1.1 404 Resource not found")); server.play();/*from w w w . jav a 2s. c om*/ URL url = server.getUrl("/users/trfields"); Request request1 = new Request.Builder().url(url).delete().build(); Response response1 = client.newCall(request1).execute(); Request request2 = new Request.Builder().url(url).get().build(); Response response2 = client.newCall(request2).execute(); JsonNode body = map.readTree(response2.body().string()); System.out.println(body.toString()); RecordedRequest recorded1 = server.takeRequest(); Assert.assertEquals(recorded1.getPath(), "/users/trfields"); Assert.assertEquals(recorded1.getMethod(), "DELETE"); RecordedRequest recorded2 = server.takeRequest(); Assert.assertEquals(recorded2.getPath(), "/users/trfields"); Assert.assertEquals(recorded2.getMethod(), "GET"); server.shutdown(); }
From source file:com.github.arven.rest.AccountServiceTests.java
@Test public void updateAccount() throws Exception { server = new MockWebServer(); server.enqueue(new MockResponse().setHeader("Content-Type", JSON) .setBody(map.writeValueAsString(new ServiceResponse(ServiceResponse.Type.SUCCESS, 200)))); server.enqueue(new MockResponse().setHeader("Content-Type", JSON) .setBody(map.writeValueAsString(new AccountInformation("trfields", "Tom")))); server.play();/*from ww w.j av a2s .c o m*/ URL url = server.getUrl("/users/trfields"); Patch.Builder patch = new Patch.Builder(); patch.test("/username", "trfields"); patch.replace("/nickname", "Tom"); RequestBody patchBody = RequestBody.create(JSON_PATCH, patch.toString()); Request request1 = new Request.Builder().url(url).patch(patchBody).build(); Response response1 = client.newCall(request1).execute(); ServiceResponse body1 = map.readValue(response1.body().string(), ServiceResponse.class); Assert.assertEquals(body1.code, 200); Assert.assertEquals(body1.type, ServiceResponse.Type.SUCCESS); Request request2 = new Request.Builder().url(url).get().build(); Response response2 = client.newCall(request2).execute(); AccountInformation body2 = map.readValue(response2.body().string(), AccountInformation.class); Assert.assertEquals(body2.nickname, "Tom"); Assert.assertEquals(body2.username, "trfields"); RecordedRequest recorded1 = server.takeRequest(); Assert.assertEquals(recorded1.getPath(), "/users/trfields"); Assert.assertEquals(recorded1.getMethod(), "PATCH"); RecordedRequest recorded2 = server.takeRequest(); Assert.assertEquals(recorded2.getPath(), "/users/trfields"); Assert.assertEquals(recorded2.getMethod(), "GET"); server.shutdown(); }
From source file:com.github.automately.sdk.Automately.java
License:Mozilla Public License
public static JsonObject getAutomatelyCloudPlan() { try {/* ww w . ja v a 2s .co m*/ JsonObject requestData = new JsonObject(); requestData.putString("username", getApiUsername()); requestData.putString("apiKey", getApiKey()); Request request = new Request.Builder() .url(USER_API_ENDPOINT + "/getPlan").post(RequestBody .create(MediaType.parse("application/json"), requestData.toString().getBytes())) .build(); Response response = httpClient.newCall(request).execute(); if (response != null) { String responseEntity = response.body().string(); try { return new JsonObject(responseEntity); } catch (DecodeException ignored) { getFormattedError("Decode Exception", "There was an issue decoding the response."); } } } catch (IOException ignored) { } return null; }
From source file:com.github.automately.sdk.Automately.java
License:Mozilla Public License
public static JsonObject getAutomatelyCloudPlans() { try {/*from ww w . j a v a 2 s . c o m*/ Request request = new Request.Builder().url(USER_API_ENDPOINT + "/getPlans").get().build(); Response response = httpClient.newCall(request).execute(); if (response != null) { String responseEntity = response.body().string(); try { return new JsonObject(responseEntity); } catch (DecodeException ignored) { getFormattedError("Decode Exception", "There was an issue decoding the response."); } } } catch (IOException ignored) { } return null; }
From source file:com.github.automately.sdk.Automately.java
License:Mozilla Public License
public static JsonObject setAutomatelyCloudPlan(String planId) { try {// w ww . j ava 2 s .c o m JsonObject requestData = new JsonObject(); requestData.putString("planId", planId); requestData.putString("username", getApiUsername()); requestData.putString("apiKey", getApiKey()); Request request = new Request.Builder() .url(USER_API_ENDPOINT + "/changePlan").post(RequestBody .create(MediaType.parse("application/json"), requestData.toString().getBytes())) .build(); Response response = httpClient.newCall(request).execute(); if (response != null) { String responseEntity = response.body().string(); try { return new JsonObject(responseEntity); } catch (DecodeException ignored) { getFormattedError("Decode Exception", "There was an issue decoding the response."); } } } catch (IOException ignored) { } return null; }
From source file:com.github.automately.sdk.Automately.java
License:Mozilla Public License
public static JsonObject registerModule(JsonObject manifest) { try {/* www .ja v a 2 s . c o m*/ JsonObject requestData = new JsonObject(); // The module register requires authentication requestData.putObject("manifest", manifest); requestData.putString("username", getApiUsername()); requestData.putString("apiKey", getApiKey()); Request request = new Request.Builder().url(REGISTRY_ENDPOINT + "/submit").post( RequestBody.create(MediaType.parse("application/json"), requestData.toString().getBytes())) .build(); Response response = httpClient.newCall(request).execute(); if (response != null) { String responseEntity = response.body().string(); try { checkAuthorized(responseEntity); return new JsonObject(responseEntity); } catch (DecodeException ignored) { getFormattedError("Decode Exception", "There was an issue decoding the response."); } } } catch (IOException ignored) { } return null; }