Example usage for com.squareup.okhttp Request newBuilder

List of usage examples for com.squareup.okhttp Request newBuilder

Introduction

In this page you can find the example usage for com.squareup.okhttp Request newBuilder.

Prototype

public Builder newBuilder() 

Source Link

Usage

From source file:pct.droid.base.providers.media.YTSProvider.java

License:Open Source License

@Override
protected Call enqueue(Request request, com.squareup.okhttp.Callback requestCallback) {
    Context context = PopcornApplication.getAppContext();
    PackageInfo pInfo;//from   w w w .j  av  a2  s.co m
    String versionName = "0.0.0";
    try {
        pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        versionName = pInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    request = request.newBuilder().removeHeader("User-Agent").addHeader("User-Agent", String.format(
            "Mozilla/5.0 (Linux; U; Android %s; %s; %s Build/%s) AppleWebkit/534.30 (KHTML, like Gecko) PT/%s",
            Build.VERSION.RELEASE, LocaleUtils.getCurrentAsString(), Build.MODEL, Build.DISPLAY, versionName))
            .build();
    return super.enqueue(request, requestCallback);
}

From source file:pedroscott.com.popularmoviesapp.rest.RestClientPublic.java

License:Apache License

private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    client.interceptors().add(interceptor);
    client.networkInterceptors().add(new Interceptor() {
        @Override//  www .  java 2 s . co m
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.httpUrl().newBuilder().addQueryParameter("api_key", key).build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);
        }
    });
    client.networkInterceptors().add(new StethoInterceptor());
    return client;
}

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()
                .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();
    }//from   w ww  . ja v a2  s.  co m
    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);
}