Search in sources :

Example 96 with Builder

use of okhttp3.MultipartBody.Builder in project DevRing by LJYcoder.

the class RingModule method okHttpClient.

// @Singleton
@Provides
OkHttpClient okHttpClient(Application application, OkHttpClient.Builder builder, HttpConfig httpConfig, HttpLoggingInterceptor loggingInterceptor, HttpCacheInterceptor cacheInterceptor, HttpHeaderInterceptor headerInterceptor, HttpProgressInterceptor progressInterceptor) {
    if (httpConfig.getConnectTimeout() > 0) {
        builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.getReadTimeout() > 0) {
        builder.readTimeout(httpConfig.getReadTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.isUseLog() && !builder.interceptors().contains(loggingInterceptor)) {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
    }
    if (httpConfig.isUseCache() && !builder.interceptors().contains(cacheInterceptor)) {
        // 缓存目录
        File cacheFile;
        if (httpConfig.getCacheFolder() != null && httpConfig.getCacheFolder().isDirectory()) {
            cacheFile = httpConfig.getCacheFolder();
        } else {
            cacheFile = FileUtil.getDirectory(FileUtil.getExternalCacheDir(application), "retrofit_http_cache");
        }
        // 大小默认20Mb
        Cache cache = new Cache(cacheFile, httpConfig.getCacheSize() > 0 ? httpConfig.getCacheSize() : 1024 * 1024 * 20);
        builder.addInterceptor(cacheInterceptor);
        builder.addNetworkInterceptor(cacheInterceptor);
        builder.cache(cache);
    }
    if (httpConfig.isUseCookie()) {
        if (httpConfig.isCookiePersistent()) {
            ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(application));
            builder.cookieJar(cookieJar);
        } else {
            CookieManager cookieManager = new CookieManager();
            cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            builder.cookieJar(new JavaNetCookieJar(cookieManager));
        }
    }
    headerInterceptor.setMapHeader(httpConfig.getMapHeader());
    if (!builder.interceptors().contains(headerInterceptor)) {
        builder.addInterceptor(headerInterceptor);
    }
    if (!builder.interceptors().contains(progressInterceptor)) {
        builder.addNetworkInterceptor(progressInterceptor);
    }
    // 配置SSL
    if (httpConfig.getSslSocketFactory() != null && httpConfig.getTrustManager() != null) {
        builder = builder.build().newBuilder().sslSocketFactory(httpConfig.getSslSocketFactory(), httpConfig.getTrustManager());
    }
    return builder.build();
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) ClearableCookieJar(com.ljy.devring.http.support.persistentcookiejar.ClearableCookieJar) PersistentCookieJar(com.ljy.devring.http.support.persistentcookiejar.PersistentCookieJar) SetCookieCache(com.ljy.devring.http.support.persistentcookiejar.cache.SetCookieCache) SharedPrefsCookiePersistor(com.ljy.devring.http.support.persistentcookiejar.persistence.SharedPrefsCookiePersistor) File(java.io.File) CookieManager(java.net.CookieManager) Cache(okhttp3.Cache) DiskCache(com.ljy.devring.cache.support.DiskCache) SpCache(com.ljy.devring.cache.support.SpCache) SetCookieCache(com.ljy.devring.http.support.persistentcookiejar.cache.SetCookieCache) MemoryCache(com.ljy.devring.cache.support.MemoryCache) Provides(dagger.Provides)

Example 97 with Builder

use of okhttp3.MultipartBody.Builder in project run-wallet-android by runplay.

the class RunIotaAPICore method postConstruct.

/**
 * added header for IRI
 */
private void postConstruct() {
    boolean USE_AUTH = false;
    // if(host.contains(".runplay.com") || host.contains(".runpg.com"))
    // USE_AUTH=true;
    final String nodeUrl = protocol + "://" + host + ":" + port;
    if (USE_AUTH) {
        // Log.e("IRI-CONNECT","Using Auth OK");
        String creds = Base64.encodeToString((uname + ":" + upassword).getBytes(), false);
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        OkHttpClient client = builder.readTimeout(5000, TimeUnit.SECONDS).addInterceptor(new Interceptor() {

            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request newRequest;
                newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).addHeader("Authorization", "Basic " + creds).build();
                return chain.proceed(newRequest);
            }
        }).connectTimeout(5000, TimeUnit.SECONDS).build();
        // use client to create Retrofit service
        final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
        service = retrofit.create(IotaAPIService.class);
    } else {
        // Log.e("IRI-CONNECT","NOOTTTTT Using Auth");
        final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).addNetworkInterceptor(new Interceptor() {

            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                final Buffer buffer = new Buffer();
                chain.request().body().writeTo(buffer);
                return chain.proceed(chain.request());
            }
        }).addInterceptor(new Interceptor() {

            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request newRequest;
                newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).build();
                return chain.proceed(newRequest);
            }
        }).connectTimeout(5000, TimeUnit.SECONDS).build();
        // use client to create Retrofit service
        final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
        service = retrofit.create(IotaAPIService.class);
    }
    // Create OkHttpBuilder
    log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
Also used : Buffer(okio.Buffer) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor)

Example 98 with Builder

use of okhttp3.MultipartBody.Builder in project bugzy by cpunq.

the class NetModule method provideGithubApiService.

@Provides
@Singleton
GithubApiService provideGithubApiService(Gson gson) {
    OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
    Retrofit.Builder builder = new Retrofit.Builder().baseUrl("https://api.github.com").addCallAdapterFactory(new LiveDataCallAdapterFactory(gson)).addConverterFactory(GsonConverterFactory.create(gson));
    ;
    Retrofit retrofit = builder.client(httpClientBuilder.build()).build();
    return retrofit.create(GithubApiService.class);
}
Also used : LiveDataCallAdapterFactory(in.bugzy.utils.LiveDataCallAdapterFactory) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 99 with Builder

use of okhttp3.MultipartBody.Builder in project bugzy by cpunq.

the class NetModule method provideFogBugzService.

@Provides
@Singleton
FogbugzApiService provideFogBugzService(Application application, PrefsHelper prefsHelper, Gson gson, HostSelectionInterceptor hostSelectionInterceptor) {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.writeTimeout(2, TimeUnit.MINUTES);
    httpClient.readTimeout(2, TimeUnit.MINUTES);
    Retrofit.Builder builder = new Retrofit.Builder().baseUrl(mBaseUrl).addCallAdapterFactory(new LiveDataCallAdapterFactory(gson)).addConverterFactory(GsonConverterFactory.create(gson));
    Retrofit retrofit = builder.client(httpClient.addInterceptor(new ConnectivityInterceptor(application.getApplicationContext())).addInterceptor(new RequestInterceptor(prefsHelper)).addInterceptor(hostSelectionInterceptor).build()).build();
    return retrofit.create(FogbugzApiService.class);
}
Also used : LiveDataCallAdapterFactory(in.bugzy.utils.LiveDataCallAdapterFactory) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) RequestInterceptor(in.bugzy.data.remote.RequestInterceptor) ConnectivityInterceptor(in.bugzy.data.remote.ConnectivityInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 100 with Builder

use of okhttp3.MultipartBody.Builder in project FlareBot by FlareBot.

the class WebUtils method post.

public static Response post(Request.Builder builder) throws IOException {
    Response res = client.newCall(builder.build()).execute();
    ResponseBody body = res.body();
    if (res.code() >= 200 && res.code() < 300)
        return res;
    else
        throw new IllegalStateException("Failed to POST to '" + builder.build().url() + "'! Code: " + res.code() + ", Message: " + res.message() + ", Body: " + (body != null ? body.string().replace("\n", "").replace("\t", " ").replaceAll(" +", " ") : "null"));
}
Also used : Response(okhttp3.Response) ResponseBody(okhttp3.ResponseBody)

Aggregations

Request (okhttp3.Request)410 OkHttpClient (okhttp3.OkHttpClient)359 Response (okhttp3.Response)287 IOException (java.io.IOException)221 RequestBody (okhttp3.RequestBody)172 HttpUrl (okhttp3.HttpUrl)109 MultipartBody (okhttp3.MultipartBody)102 File (java.io.File)93 Test (org.junit.Test)92 Map (java.util.Map)91 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)87 Call (okhttp3.Call)77 HashMap (java.util.HashMap)63 AndroidFlipperClient (com.facebook.flipper.android.AndroidFlipperClient)59 FlipperClient (com.facebook.flipper.core.FlipperClient)59 DatabasesFlipperPlugin (com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin)59 FrescoFlipperPlugin (com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin)59 InspectorFlipperPlugin (com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin)59 NetworkFlipperPlugin (com.facebook.flipper.plugins.network.NetworkFlipperPlugin)59 ReactFlipperPlugin (com.facebook.flipper.plugins.react.ReactFlipperPlugin)59