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.auth0.api.internal.ApplicationInfoRequest.java
License:Open Source License
@Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) { String message = "Received app info failed response with code " + response.code() + " and body " + response.body().string(); postOnFailure(new IOException(message)); return;//from w w w . j av a 2s . c o m } try { String json = response.body().string(); JSONTokener tokenizer = new JSONTokener(json); tokenizer.skipPast("Auth0.setClient("); if (!tokenizer.more()) { postOnFailure(tokenizer.syntaxError("Invalid App Info JSONP")); return; } Object nextValue = tokenizer.nextValue(); if (!(nextValue instanceof JSONObject)) { tokenizer.back(); postOnFailure(tokenizer.syntaxError("Invalid JSON value of App Info")); } JSONObject jsonObject = (JSONObject) nextValue; Log.d(TAG, "Obtained JSON object from JSONP: " + jsonObject); Application app = getReader().readValue(jsonObject.toString()); postOnSuccess(app); } catch (JSONException | IOException e) { postOnFailure(new APIClientException("Failed to parse JSONP", e)); } }
From source file:com.auth0.api.internal.SimpleRequest.java
License:Open Source License
@Override public void onResponse(Response response) throws IOException { Log.d(TAG, String.format("Received response from request to %s with status code %d", response.request().urlString(), response.code())); final InputStream byteStream = response.body().byteStream(); if (!response.isSuccessful()) { Throwable throwable;//w w w . ja v a 2 s . co m try { Map<String, Object> payload = errorReader.readValue(byteStream); throwable = new APIClientException("Request failed with response " + payload, response.code(), payload); } catch (IOException e) { throwable = new APIClientException("Request failed", response.code(), null); } postOnFailure(throwable); return; } try { Log.d(TAG, "Received successful response from " + response.request().urlString()); T payload = getReader().readValue(byteStream); postOnSuccess(payload); } catch (IOException e) { postOnFailure(new APIClientException("Request failed", response.code(), null)); } }
From source file:com.auth0.api.internal.VoidRequest.java
License:Open Source License
@Override public void onResponse(Response response) throws IOException { Log.d(TAG, String.format("Received response from request to %s with status code %d", response.request().urlString(), response.code())); final InputStream byteStream = response.body().byteStream(); if (!response.isSuccessful()) { Throwable throwable;/*from www. ja v a 2s . c om*/ try { Map<String, Object> payload = errorReader.readValue(byteStream); throwable = new APIClientException("Request failed with response " + payload, response.code(), payload); } catch (IOException e) { throwable = new APIClientException("Request failed", response.code(), null); } postOnFailure(throwable); return; } postOnSuccess(null); }
From source file:com.auth0.request.internal.BaseRequest.java
License:Open Source License
protected APIException parseUnsuccessfulResponse(Response response) { try {// ww w . j a va 2 s .c om final InputStream byteStream = response.body().byteStream(); Map<String, Object> payload = errorReader.readValue(byteStream); return new APIException("Request to " + url + " failed with response " + payload, response.code(), payload); } catch (Exception e) { return new APIException("Request to " + url + " failed", response.code(), null); } }
From source file:com.auth0.request.internal.SimpleRequest.java
License:Open Source License
@Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) { APIException exception = parseUnsuccessfulResponse(response); postOnFailure(exception);/*from w ww . j av a 2 s .c o m*/ return; } try { final InputStream byteStream = response.body().byteStream(); T payload = getReader().readValue(byteStream); postOnSuccess(payload); } catch (IOException e) { postOnFailure(new Auth0Exception("Failed to parse response to request to " + url, e)); } }
From source file:com.auth0.request.internal.SimpleRequest.java
License:Open Source License
@Override public T execute() throws Auth0Exception { Request request = doBuildRequest(newBuilder()); Response response; try {/*ww w .j a v a 2 s . c o m*/ response = client.newCall(request).execute(); } catch (IOException e) { throw new Auth0Exception("Failed to execute request to " + url, e); } if (!response.isSuccessful()) { throw parseUnsuccessfulResponse(response); } try { final InputStream byteStream = response.body().byteStream(); return getReader().readValue(byteStream); } catch (IOException e) { throw new Auth0Exception("Failed to parse response to request to " + url, e); } }
From source file:com.ayuget.redface.network.PageFetcher.java
License:Apache License
public Observable<String> fetchSource(final User user, final String pageUrl) { return Observable.create(new Observable.OnSubscribe<String>() { @Override//from ww w . j a v a 2 s . com public void call(Subscriber<? super String> subscriber) { // Obtain the HttpClient associated with the current User. Having different clients for // each user allows us to easily deal with cookies and to support multi-users in the app OkHttpClient client = httpClientProvider.getClientForUser(user); CacheControl cacheControl = new CacheControl.Builder().noTransform().build(); Request request = new Request.Builder().cacheControl(cacheControl).url(pageUrl).build(); try { Response response = client.newCall(request).execute(); subscriber.onNext(response.body().string()); subscriber.onCompleted(); } catch (IOException e) { subscriber.onError(e); } } }); }
From source file:com.ayuget.redface.ui.misc.ImageMenuHandler.java
License:Apache License
/** * Saves image from network using OkHttp. Picasso is not used because it would strip away the * EXIF data once the image is saved (Picasso directly gives us a Bitmap). *///from w ww . j a va 2 s .c om private void saveImageFromNetwork(final File mediaFile, final Bitmap.CompressFormat targetFormat, final boolean compressAsPng, final boolean notifyUser, final boolean broadcastSave, final ImageSavedCallback imageSavedCallback) { OkHttpClient okHttpClient = new OkHttpClient(); final Request request = new Request.Builder().url(imageUrl).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { SnackbarHelper.makeError(activity, R.string.error_saving_image).show(); } @Override public void onResponse(Response response) throws IOException { final byte[] imageBytes = response.body().bytes(); Timber.d("Image successfully decoded, requesting WRITE_EXTERNAL_STORAGE permission to save image"); RxPermissions.getInstance(activity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE) .subscribe(new Action1<Boolean>() { @Override public void call(Boolean granted) { if (granted) { Timber.d("WRITE_EXTERNAL_STORAGE granted, saving image to disk"); try { Timber.d("Saving image to %s", mediaFile.getAbsolutePath()); if (compressAsPng) { Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); StorageHelper.storeImageToFile(bitmap, mediaFile, targetFormat); } else { StorageHelper.storeImageToFile(imageBytes, mediaFile); } if (broadcastSave) { // First, notify the system that a new image has been saved // to external storage. This is important for user experience // because it makes the image visible in the system gallery // app. StorageHelper.broadcastImageWasSaved(activity, mediaFile, targetFormat); } if (notifyUser) { // Then, notify the user with an enhanced snackbar, allowing // him (or her) to open the image in his favorite app. Snackbar snackbar = SnackbarHelper.makeWithAction(activity, R.string.image_saved_successfully, R.string.action_snackbar_open_image, new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType( Uri.parse("file://" + mediaFile.getAbsolutePath()), "image/*"); activity.startActivity(intent); } }); snackbar.show(); } notifyImageWasSaved(imageSavedCallback, mediaFile, targetFormat); } catch (IOException e) { Timber.e(e, "Unable to save image to external storage"); SnackbarHelper.makeError(activity, R.string.error_saving_image).show(); } } else { Timber.w("WRITE_EXTERNAL_STORAGE denied, unable to save image"); SnackbarHelper .makeError(activity, R.string.error_saving_image_permission_denied) .show(); } } }); } }); }
From source file:com.baasbox.android.net.OkClient.java
License:Apache License
private HttpEntity asEntity(Response resp) { BasicHttpEntity entity = new BasicHttpEntity(); InputStream inputStream = resp.body().byteStream(); entity.setContent(inputStream);/*from www. j ava2 s . c o m*/ entity.setContentLength(resp.body().contentLength()); String ctnt = resp.body().contentType().toString(); entity.setContentType(ctnt); return entity; }
From source file:com.bartoszlipinski.streetviewprobe.StreetViewProbe.java
License:Apache License
public synchronized void probe(final double lat, final double lon, final OnStreetViewStatusListener listener) { if (listener == null) { throw new IllegalArgumentException("Listener must not be null."); }//from www. j a va 2 s .c om new Thread(new Runnable() { @Override public void run() { if (context.get() != null) { String path = context.get().getString(R.string.probe_path, size, lat, lon); client.newCall(new Request.Builder().url(path).build()).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { listener.onStreetViewStatus(Status.UNKNOWN); } @Override public void onResponse(Response response) throws IOException { // Log.d("BITM", response.body().contentLength() + " "); listener.onStreetViewStatus( response.body().contentLength() > threshold ? Status.AVAILABLE : Status.UNAVAILABLE); } }); } } }).start(); }