List of usage examples for com.squareup.okhttp Request headers
Headers headers
To view the source code for com.squareup.okhttp Request headers.
Click Source Link
From source file:mobi.lab.sample_event_logging_library.service.LogPostService.java
License:Open Source License
@Override public void onCreate() { super.onCreate(); //TODO: starting from Android 5.0 we could use JobScheduler (currently not in support libraries) handler = new Handler(); handler.postDelayed(timedRunnable, Config.MAX_TIME_INTERVAL_BETWEEN_REQUESTS); logEventsForRequest = new ArrayList<>(); OkHttpClient client = new OkHttpClient(); client.interceptors().add(new Interceptor() { @Override/*from www . j a v a2s .c om*/ public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { Request request = chain.request(); Log.d(TAG, String.format("\n\nrequest:%s\nheaders:%s\n" + "url:%s", request.body().toString(), request.headers(), request.httpUrl().toString())); return chain.proceed(request); } }); Retrofit retrofit = new Retrofit.Builder().baseUrl(Network.BASE_API_URL) .addConverterFactory(GsonConverterFactory.create()).client(client).build(); logEventService = retrofit.create(LogEventRequest.class); if (checkCallingOrSelfPermission( Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkCallingOrSelfPermission( Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //TODO: ask for permission if not given Log.d(TAG, "location permission not granted"); isLocationServicesEnabled = false; return; } isLocationServicesEnabled = true; final LocationManager locationManager = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE); final Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); Log.d(TAG, "requesting user location"); locationManager.requestLocationUpdates(Config.LOCATION_UPDATES_MIN_TIME, Config.LOCATION_UPDATES_MIN_DISTANCE, criteria, this, null); }
From source file:name.kevinlocke.appveyor.testutils.ConcurrentHttpLoggingInterceptor.java
License:Apache License
@Override public Response intercept(Chain chain) throws IOException { Level level = this.level; Request request = chain.request(); if (level == Level.NONE) { return chain.proceed(request); }/* ww w. j a va 2s . c o m*/ boolean logBody = level == Level.BODY; boolean logHeaders = logBody || level == Level.HEADERS; RequestBody requestBody = request.body(); Connection connection = chain.connection(); Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1; UUID requestId = UUID.randomUUID(); StringBuilder requestMessage = new StringBuilder("--> ").append(requestId).append('\n') .append(request.method()).append(' ').append(request.httpUrl()).append(' ').append(protocol); if (!logHeaders && requestBody != null) { requestMessage.append(" (").append(requestBody.contentLength()).append("-byte body)"); } requestMessage.append('\n'); if (logHeaders) { if (requestBody != null) { // Request body headers are only present when installed as a // network interceptor. Force // them to be included (when available) so there values are // known. if (requestBody.contentType() != null) { requestMessage.append("Content-Type: ").append(requestBody.contentType()).append('\n'); } if (requestBody.contentLength() != -1) { requestMessage.append("Content-Length: ").append(requestBody.contentLength()).append('\n'); } } Headers headers = request.headers(); for (int i = 0, count = headers.size(); i < count; i++) { String name = headers.name(i); if ("Authorization".equalsIgnoreCase(name) || "Proxy-Authenticate".equalsIgnoreCase(name) || "Proxy-Authorization".equalsIgnoreCase(name) || "WWW-Authenticate".equalsIgnoreCase(name)) { requestMessage.append(name).append(": *****\n"); } // Skip headers from the request body as they are explicitly // logged above. else if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) { requestMessage.append(name).append(": ").append(headers.value(i)).append('\n'); } } if (!logBody || requestBody == null) { requestMessage.append("--> END ").append(requestId).append('\n'); } else if (bodyEncoded(request.headers())) { requestMessage.append("--> END ").append(requestId).append(" (encoded body omitted)").append('\n'); } else { Buffer buffer = new Buffer(); requestBody.writeTo(buffer); Charset charset = UTF8; MediaType contentType = requestBody.contentType(); if (contentType != null) { charset = contentType.charset(UTF8); } requestMessage.append('\n'); if (isPlaintext(buffer)) { requestMessage.append(buffer.readString(charset)).append("\n--> END ").append(requestId) .append(" (").append(requestBody.contentLength()).append("-byte body)\n"); } else { requestMessage.append("--> END ").append(requestId).append(" (binary ") .append(requestBody.contentLength()).append("-byte body omitted)\n"); } } } logger.log(requestMessage.substring(0, requestMessage.length() - 1)); long startNs = System.nanoTime(); Response response; try { response = chain.proceed(request); } catch (Exception e) { logger.log("<-- " + requestId + "HTTP FAILED: " + e); throw e; } long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); ResponseBody responseBody = response.body(); long contentLength = responseBody.contentLength(); StringBuilder responseMessage = new StringBuilder("<-- ").append(requestId).append(' ') .append(response.request().url()).append(" (").append(tookMs).append("ms"); if (!logHeaders) { responseMessage.append(", "); if (contentLength != -1) { responseMessage.append(contentLength).append("-byte"); } else { responseMessage.append("unknown-length"); } responseMessage.append(" body"); } responseMessage.append(")\n"); responseMessage.append(response.code()).append(' ').append(response.message()).append('\n'); if (logHeaders) { Headers headers = response.headers(); for (int i = 0, count = headers.size(); i < count; i++) { responseMessage.append(headers.name(i)).append(": ").append(headers.value(i)).append('\n'); } if (!logBody || !HttpEngine.hasBody(response)) { responseMessage.append("<-- END HTTP\n"); } else if (bodyEncoded(response.headers())) { responseMessage.append("<-- END HTTP (encoded body omitted)\n"); } else { BufferedSource source = responseBody.source(); source.request(Long.MAX_VALUE); // Buffer the entire body. Buffer buffer = source.buffer(); Charset charset = UTF8; MediaType contentType = responseBody.contentType(); if (contentType != null) { charset = contentType.charset(UTF8); } if (!isPlaintext(buffer)) { responseMessage.append('\n').append("<-- END HTTP (binary ").append(buffer.size()) .append("-byte body omitted)"); logger.log(responseMessage.toString()); return response; } if (contentLength != 0) { responseMessage.append('\n').append(buffer.clone().readString(charset)).append('\n'); } responseMessage.append("<-- END HTTP (").append(buffer.size()).append("-byte body)\n"); } } logger.log(responseMessage.substring(0, responseMessage.length() - 1)); return response; }
From source file:net.goldenspiral.fetlifeoss.rest.interceptors.LoggingInterceptor.java
License:Open Source License
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); if (enabled) { Log.i(TAG, String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); }//from ww w. jav a 2s . c om Response response = chain.proceed(request); long t2 = System.nanoTime(); if (enabled) { Log.i(TAG, String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); } return response; }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
/** * ============Delivery============/* ww w . ja v a 2s .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============/* ww w . j ava2s . c om*/ */ 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);//from w w w .j a v a2 s .c o m 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:ooo.oxo.mr.net.LoggingInterceptor.java
License:Open Source License
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Log.d(TAG, String.format("%s\n%s", request, request.headers())); Response response = chain.proceed(request); Log.d(TAG, String.format("%s\n%s", response, response.headers())); return response; }
From source file:org.addhen.birudo.data.net.LoggingInterceptor.java
License:Apache License
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); Timber.i(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); Timber.i(String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers()));/*from w ww .j av a 2 s. c o m*/ return response; }
From source file:org.apache.nifi.processors.standard.InvokeHTTP.java
License:Apache License
private void logRequest(ComponentLog logger, Request request) { logger.debug("\nRequest to remote service:\n\t{}\n{}", new Object[] { request.url().toExternalForm(), getLogString(request.headers().toMultimap()) }); }