List of usage examples for com.squareup.okhttp Request toString
@Override
public String toString()
From source file:cc.arduino.mvd.services.HttpService.java
License:Apache License
/** * Perform a POST request to the service * * @param url/* w w w . j a va2 s .co m*/ * @param json * @return * @throws IOException */ private void post(String url, String json) { MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, json); Log.d(TAG, "POST: " + url); final Request request = new Request.Builder().url(url).post(body).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException exception) { // Meh, don't do anything... exception.printStackTrace(); } @Override public void onResponse(final Response response) throws IOException { // Meh, don't do anything... Log.d(TAG, request.toString()); } }); }
From source file:com.digitalglobe.iipfoundations.productservice.orderservice.OrderService.java
/** * /*from w ww .ja va 2s .c o m*/ * @param cat_id * @param auth_token * @return - the order_id * @throws IOException * @throws OrderServiceException */ public static String order1b(String cat_id, String auth_token) throws IOException, OrderServiceException { String request = generateOrder1bRequestBody(cat_id); System.out.println(request); MediaType mediaType = MediaType.parse("application/json"); RequestBody request_body = RequestBody.create(mediaType, request); //Properties gbdx = GBDxCredentialManager.getGBDxCredentials(); Request search_request = new Request.Builder().url("http://orders.iipfoundations.com/order/v1") .post(request_body).addHeader("content-type", "application/json") .addHeader("authorization", "Basic " + auth_token).addHeader("username", username) .addHeader("password", password).build(); OkHttpClient client = new OkHttpClient(); System.out.println(search_request.toString()); Response response = client.newCall(search_request).execute(); if (200 == response.code()) { String body = response.body().string(); System.out.println(body); JSONObject obj = new JSONObject(body); JSONArray orders = obj.getJSONArray("orders"); JSONObject order = orders.getJSONObject(0); int id = order.getInt("id"); return Integer.toString(id); } else { System.out.println(response.body().string()); logger.error(response.message()); throw new OrderServiceException(response.message()); } }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
/** * ============Delivery============//from ww w .jav a 2 s . c o m */ protected void deliveryAsyncResult(Request request, HttpCallback<?> callback) { Util.log("onDelivery:" + request.url().toString()); Util.log("Headers:\n" + request.headers().toString()); final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback; //Call start callStart(resCallBack, request); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException e) { Util.log("onFailure:" + request.toString()); callFailure(resCallBack, request, null, e); callFinish(resCallBack); } @Override public void onResponse(final Response response) { try { Object ret = null; final String string = response.body().string(); final boolean haveValue = !TextUtils.isEmpty(string); Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null")); if (haveValue) { ret = mResolver.analysis(string, resCallBack.getClass()); } callSuccess(resCallBack, ret, response.code()); } catch (Exception e) { Util.log("onResponse Failure:" + response.request().toString()); callFailure(resCallBack, response.request(), response, e); } callFinish(resCallBack); } }); }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
/** * ============Delivery============/*from w w w . j a v a 2 s. c o m*/ */ protected void deliveryAsyncResult(OkHttpClient client, Request request, HttpCallback<?> callback) { Util.log("onDelivery:" + request.url().toString()); Util.log("Headers:\n" + request.headers().toString()); final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback; //Call start callStart(resCallBack, request); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException e) { Util.log("onFailure:" + request.toString()); callFailure(resCallBack, request, null, e); callFinish(resCallBack); } @Override public void onResponse(final Response response) { try { Object ret = null; final String string = response.body().string(); final boolean haveValue = !TextUtils.isEmpty(string); Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null")); if (haveValue) { ret = mResolver.analysis(string, resCallBack.getClass()); } callSuccess(resCallBack, ret, response.code()); } catch (Exception e) { Util.log("onResponse Failure:" + response.request().toString()); callFailure(resCallBack, response.request(), response, e); } callFinish(resCallBack); } }); }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
protected <T> T deliveryResult(Class<T> tClass, Request request, HttpCallback<?> callback) { Util.log("onDelivery:" + request.url().toString()); Util.log("Headers:\n" + request.headers().toString()); if (callback == null && tClass == null) callback = new DefaultCallback(); final Class<?> subClass = tClass == null ? callback.getClass() : tClass; callStart(callback, request);// w ww . j a v a2s .com Call call = mOkHttpClient.newCall(request); Response response = null; Object ret = null; try { response = call.execute(); final String string = response.body().string(); final boolean haveValue = !TextUtils.isEmpty(string); Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null")); if (haveValue) { ret = mResolver.analysis(string, subClass); } callSuccess(callback, ret, response.code()); } catch (Exception e) { Request req = response == null ? request : response.request(); Util.log("onResponse Failure:" + req.toString()); callFailure(callback, req, response, e); } callFinish(callback); return (T) ret; }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
protected void deliveryAsyncResult(final Request request, final StreamCall call, final HttpCallback<?> callback) { Util.log("onDelivery:" + request.url().toString()); final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback; //Call start//w w w .java 2 s. co m callStart(resCallBack, request); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException e) { Util.log("onFailure:" + request.toString()); callFailure(resCallBack, request, null, e); callFinish(resCallBack); } @Override public void onResponse(final Response response) { OutputStream out = call.getOutputStream(); InputStream in = null; byte[] buf = new byte[mBufferSize]; try { Util.log("onResponse:Code:%d Stream.", response.code()); ResponseBody body = response.body(); bindResponseProgressCallback(request.body(), body, callback); in = body.byteStream(); int size; while ((size = in.read(buf)) != -1) { out.write(buf, 0, size); out.flush(); } // On success call.onSuccess(response.code()); } catch (Exception e) { Util.log("onResponse Failure:" + response.request().toString()); callFailure(resCallBack, response.request(), response, e); } finally { com.squareup.okhttp.internal.Util.closeQuietly(in); com.squareup.okhttp.internal.Util.closeQuietly(out); } callFinish(resCallBack); } }); }
From source file:ooo.oxo.moments.InstaApplication.java
License:Open Source License
@Override public void onCreate() { super.onCreate(); //noinspection ConstantConditions if (TextUtils.isEmpty(BuildConfig.CLIENT_ID) || TextUtils.isEmpty(BuildConfig.CLIENT_SECRET)) { throw new IllegalStateException("Please create a \"client.properties\" in the same" + " directory of this module to specify your CLIENT_ID and CLIENT_SECRET."); }//w w w .jav a2s . c o m InstaSharedState.createInstance(this); httpClient = new OkHttpClient(); httpClient.interceptors().add(chain -> { Request request = chain.request(); Log.d("OkHttp", request.toString()); Response response = chain.proceed(request); Log.d("OkHttp", response.toString()); return response; }); InstaSharedState.getInstance().applyProxy(); Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .registerTypeAdapter(Date.class, new TimestampTypeAdapter()).create(); retrofit = new Retrofit.Builder().client(httpClient).addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl("https://api.instagram.com/") .build(); }
From source file:syncthing.api.SyncthingApiInterceptor.java
License:Open Source License
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (!StringUtils.isEmpty(config.getApiKey())) { request = request.newBuilder()/*from ww w .j a va 2s . co m*/ .addHeader(SyncthingApi.HEADER_API_KEY, StringUtils.trim(config.getApiKey())).build(); } else if (!StringUtils.isEmpty(config.getAuth())) { request = request.newBuilder().addHeader("Authorization", StringUtils.trim(config.getAuth())).build(); } if (config.isDebug()) { Timber.d(request.toString()); if (StringUtils.equalsIgnoreCase(request.method(), "POST")) { Buffer buffer = new Buffer(); request.body().writeTo(buffer); ByteString content = buffer.snapshot(); Timber.d("body=%s", buffer.readString(Charset.defaultCharset())); MediaType type = request.body().contentType(); request = request.newBuilder().post(RequestBody.create(type, content)).build(); } } return chain.proceed(request); }