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.j1024.mcommon.network.Http.java
License:Open Source License
public static <T> void get(final String url, final JsonArrayCallback<T> jsonArrayCallback) { L.d(TAG, "Do GET --> " + url); final Request request = new Request.Builder().url(url).tag(url).build(); client.newCall(request).enqueue(new Callback() { @Override/*from ww w. j a va 2s. c o m*/ public void onFailure(final Request request, final IOException e) { handler.post(new Runnable() { @Override public void run() { jsonArrayCallback.onFailure(request, ResultCode.NetworkException, e); } }); } @Override public void onResponse(Response response) { try { final String responseString = response.body().string(); L.v(TAG, "[" + url + "] response:" + responseString); Gson gson = new Gson(); Type token = $Gson$Types.newParameterizedTypeWithOwner(null, ArrayList.class, jsonArrayCallback.getGenericType()); final List<T> list = gson.fromJson(responseString, token); handler.post(new Runnable() { @Override public void run() { jsonArrayCallback.onResponse(list); } }); } catch (final IOException e) { handler.post(new Runnable() { @Override public void run() { jsonArrayCallback.onFailure(request, ResultCode.NetworkException, e); } }); } catch (final JsonSyntaxException e) { handler.post(new Runnable() { @Override public void run() { jsonArrayCallback.onFailure(request, ResultCode.ServerException, e); } }); } } }); }
From source file:com.j1024.mcommon.network.Http.java
License:Open Source License
public static <T> void get(final String url, final JsonObjectCallback<T> jsonObjectCallback) { L.d(TAG, "Do GET --> " + url); final Request request = new Request.Builder().url(url).tag(url).build(); client.newCall(request).enqueue(new Callback() { @Override/*from w w w . j av a 2 s.c om*/ public void onFailure(final Request request, final IOException e) { handler.post(new Runnable() { @Override public void run() { jsonObjectCallback.onFailure(request, ResultCode.NetworkException, e); } }); } @Override public void onResponse(Response response) throws IOException { try { final String responseString = response.body().string(); L.v(TAG, "[" + url + "] response:" + responseString); Gson gson = new Gson(); Class<T> clazz = jsonObjectCallback.getGenericType(); final T respObj = gson.fromJson(responseString, clazz); handler.post(new Runnable() { @Override public void run() { jsonObjectCallback.onResponse(respObj); } }); } catch (final IOException e) { handler.post(new Runnable() { @Override public void run() { jsonObjectCallback.onFailure(request, ResultCode.NetworkException, e); } }); } catch (final JsonSyntaxException e) { handler.post(new Runnable() { @Override public void run() { jsonObjectCallback.onFailure(request, ResultCode.ServerException, e); } }); } } }); }
From source file:com.janela.mobile.util.HttpImageGetter.java
License:Apache License
@Override public Drawable getDrawable(final String source) { try {//from www. ja v a2 s .c o m Drawable repositoryImage = requestRepositoryImage(source); if (repositoryImage != null) return repositoryImage; } catch (Exception e) { // Ignore and attempt request over regular HTTP request } try { Request request = new Request.Builder().url(source).build(); Response response = client.newCall(request).execute(); Bitmap bitmap = ImageUtils.getBitmap(response.body().bytes(), width, MAX_VALUE); if (bitmap == null) return loading.getDrawable(source); BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); return drawable; } catch (IOException e) { return loading.getDrawable(source); } }
From source file:com.jaspersoft.android.jaspermobile.webview.intercept.okhttp.OkResponseMapper.java
License:Open Source License
private InputStream extractData(Response response) { ResponseBody body = response.body(); if (body == null) { return null; }//w ww . j a va2s . co m try { return body.byteStream(); } catch (IOException e) { return null; } }
From source file:com.joeyfrazee.nifi.reporting.HttpProvenanceReporter.java
License:Apache License
private void post(String json, String url) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder().url(url).post(body).build(); Response response = getHttpClient().newCall(request).execute(); getLogger().info("{} {} {}", new Object[] { Integer.valueOf(response.code()), response.message(), response.body().string() }); }
From source file:com.kubeiwu.easyandroid.easyhttp.core.retrofit.KOkHttpCall.java
License:Apache License
private Response<T> parseResponse(com.squareup.okhttp.Response rawResponse, com.squareup.okhttp.Request request) throws IOException { ResponseBody rawBody = rawResponse.body(); // rawResponse.r // Remove the body's source (the only stateful object) so we can pass // the response along. rawResponse = rawResponse.newBuilder() .body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength())).build(); int code = rawResponse.code(); if (code < 200 || code >= 300) { try {/* ww w .jav a 2 s .c om*/ // Buffer the entire body to avoid future I/O. ResponseBody bufferedBody = Utils.readBodyToBytesIfNecessary(rawBody); return Response.error(bufferedBody, rawResponse); } finally { closeQuietly(rawBody); } } if (code == 204 || code == 205) { return Response.success(null, rawResponse); } ExceptionCatchingRequestBody catchingBody = new ExceptionCatchingRequestBody(rawBody); try { T body; if (responseConverter instanceof GsonConverter) { GsonConverter<T> converter = (GsonConverter<T>) responseConverter; body = converter.fromBody(catchingBody, request); } else { body = responseConverter.fromBody(catchingBody); } return Response.success(body, rawResponse); } catch (RuntimeException e) { // If the underlying source threw an exception, propagate that // rather than indicating it was // a runtime exception. catchingBody.throwIfCaught(); throw e; } }
From source file:com.librelio.activity.StartupActivity.java
License:Apache License
private void loadAdvertisingImage() { client.setReadTimeout(2, TimeUnit.SECONDS); Request request = new Request.Builder().url(getAdvertisingImageURL()).build(); client.newCall(request).enqueue(new Callback() { @Override/*w ww . j av a 2 s . c o m*/ public void onResponse(Response response) throws IOException { if (response.code() == 200) { EasyTracker.getTracker() .sendView("Interstitial/" + FilenameUtils.getName(getAdvertisingImageURL())); byte[] bytes = response.body().bytes(); if (bytes != null) { adImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } loadAdvertisingLinkAndDisplayAdvertising(); } else { showMagazineAfterDelay(DEFAULT_ADV_DELAY); } response.body().close(); } @Override public void onFailure(Request request, Throwable throwable) { showMagazineAfterDelay(DEFAULT_ADV_DELAY); } }); }
From source file:com.librelio.activity.StartupActivity.java
License:Apache License
private void loadAdvertisingLinkAndDisplayAdvertising() { Request request = new Request.Builder().url(getAdvertisingLinkURL()).build(); client.newCall(request).enqueue(new Callback() { @Override// w ww .ja v a 2s .c o m public void onResponse(Response response) throws IOException { if (response.code() == 200) { PListXMLHandler handler = new PListXMLHandler(); PListXMLParser parser = new PListXMLParser(); parser.setHandler(handler); parser.parse(response.body().string()); PList list = ((PListXMLHandler) parser.getHandler()).getPlist(); if (list != null) { Dict dict = (Dict) list.getRootElement(); String delay = dict.getString(PLIST_DELAY).getValue().toString(); String link = dict.getString(PLIST_LINK).getValue().toString(); if (adImage != null) { runOnUiThread(new Runnable() { @Override public void run() { advertisingImage.setImageBitmap(adImage); if (isFirstImage) { applyRotation(0, 90); isFirstImage = !isFirstImage; } else { applyRotation(0, -90); isFirstImage = !isFirstImage; } } }); } setOnAdvertisingImageClickListener(link); showMagazineAfterDelay(Integer.valueOf(delay)); } } else { showMagazineAfterDelay(DEFAULT_ADV_DELAY); } response.body().close(); } @Override public void onFailure(Request request, Throwable throwable) { showMagazineAfterDelay(DEFAULT_ADV_DELAY); } }); }
From source file:com.lidroid.xutils.HttpUtils.java
License:Apache License
public <T> void sendAsync(HttpRequest.HttpMethod method, String url, RequestParams params, final RequestCallBack<T> callBack) { Request.Builder builder = buildRequest(method, url, params); mOkHttpClient.newCall(builder.build()).enqueue(new Callback() { @Override//from w ww . j a v a 2 s .c o m public void onFailure(Request request, IOException e) { callBack.onFailure(new HttpException(), e.getMessage()); } @Override public void onResponse(Response response) throws IOException { if (response != null && response.body() != null) { callBack.onSuccess(new ResponseInfo<T>(null, (T) response.body().string(), false)); } else { callBack.onFailure(new HttpException("Response is null"), "Response is null"); } } }); }
From source file:com.liferay.mobile.android.auth.CookieSignIn.java
License:Open Source License
protected static Session parseResponse(Response response, String server, CookieManager cookieManager, CookieAuthentication authentication) throws Exception { if (response.code() == 500) { throw new AuthenticationException("Cookie invalid or empty"); }/*from w ww. jav a 2 s .co m*/ String body = response.body().string(); String authToken = parseAuthToken(body); String cookieHeader = getHttpCookies(cookieManager.getCookieStore()); if (Validator.isNotNull(cookieHeader)) { authentication.setAuthToken(authToken); authentication.setCookieHeader(cookieHeader); authentication.setLastCookieRefresh(System.currentTimeMillis()); return new SessionImpl(server, authentication); } else { throw new AuthenticationException("Cookie invalid or empty"); } }