Search in sources :

Example 81 with Builder

use of okhttp3.Request.Builder in project Tusky by tuskyapp.

the class InstanceSwitchAuthInterceptor method intercept.

@Override
public Response intercept(@NonNull Chain chain) throws IOException {
    Request originalRequest = chain.request();
    AccountEntity currentAccount = accountManager.getActiveAccount();
    Request.Builder builder = originalRequest.newBuilder();
    String instanceHeader = originalRequest.header(MastodonApi.DOMAIN_HEADER);
    if (instanceHeader != null) {
        // use domain explicitly specified in custom header
        builder.url(swapHost(originalRequest.url(), instanceHeader));
        builder.removeHeader(MastodonApi.DOMAIN_HEADER);
    } else if (currentAccount != null) {
        // use domain of current account
        builder.url(swapHost(originalRequest.url(), currentAccount.getDomain())).header("Authorization", String.format("Bearer %s", currentAccount.getAccessToken()));
    }
    Request newRequest = builder.build();
    return chain.proceed(newRequest);
}
Also used : Request(okhttp3.Request) AccountEntity(com.keylesspalace.tusky.db.AccountEntity)

Example 82 with Builder

use of okhttp3.Request.Builder in project lzc_app_lib by httplzc.

the class HttpUtil method download.

// 下载文件
public static void download(String url, RequestParams params, FileDownloadCallBack fileDownloadCallBack, Object tag) {
    if (!validUrl(url)) {
        fileDownloadCallBack.onFailure(Invalid, null);
        return;
    }
    HttpUrl httpUrl = formatUrl(params, url);
    if (httpUrl == null) {
        fileDownloadCallBack.onFailure(Invalid, null);
        return;
    }
    try {
        Request.Builder builder = new Request.Builder().url(httpUrl).tag(tag);
        if (params != null)
            for (Map.Entry<String, String> stringStringEntry : params.getHeads().entrySet()) {
                builder.addHeader(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        Request request = builder.build();
        OkHttpInstance.getClient().newCall(request).enqueue(fileDownloadCallBack);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Example 83 with Builder

use of okhttp3.Request.Builder in project lzc_app_lib by httplzc.

the class HttpUtil method get.

/**
 * get 有参数
 *
 * @param urlString
 * @param params
 * @param responseHandler
 */
public static void get(String urlString, RequestParams params, Callback responseHandler, Object tag) {
    HttpUrl httpUrl = formatUrl(params, urlString);
    if (httpUrl == null) {
        responseHandler.onFailure(null, null);
        return;
    }
    try {
        Request.Builder builder = new Request.Builder().url(httpUrl).tag(tag);
        if (params != null) {
            for (Map.Entry<String, String> stringStringEntry : params.getHeads().entrySet()) {
                builder.addHeader(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }
        Request request = builder.build();
        OkHttpInstance.getClient().newCall(request).enqueue(responseHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Request(okhttp3.Request) Map(java.util.Map) HttpUrl(okhttp3.HttpUrl)

Example 84 with Builder

use of okhttp3.Request.Builder in project crnk-framework by crnk-project.

the class OkHttpAdapter method initImpl.

private synchronized void initImpl() {
    if (impl == null) {
        Builder builder = new OkHttpClient.Builder();
        if (networkTimeout != null) {
            builder.readTimeout(networkTimeout, TimeUnit.MILLISECONDS);
        }
        for (OkHttpAdapterListener listener : listeners) {
            listener.onBuild(builder);
        }
        impl = builder.build();
    }
}
Also used : Builder(okhttp3.OkHttpClient.Builder)

Example 85 with Builder

use of okhttp3.Request.Builder in project crnk-framework by crnk-project.

the class AbstractMetaJerseyTest method setNetworkTimeout.

public static void setNetworkTimeout(CrnkClient client, final int timeout, final TimeUnit timeUnit) {
    OkHttpAdapter httpAdapter = (OkHttpAdapter) client.getHttpAdapter();
    httpAdapter.addListener(new OkHttpAdapterListenerBase() {

        @Override
        public void onBuild(Builder builder) {
            builder.readTimeout(timeout, timeUnit);
        }
    });
}
Also used : Builder(okhttp3.OkHttpClient.Builder) OkHttpAdapter(io.crnk.client.http.okhttp.OkHttpAdapter) OkHttpAdapterListenerBase(io.crnk.client.http.okhttp.OkHttpAdapterListenerBase)

Aggregations

Request (okhttp3.Request)170 Response (okhttp3.Response)120 OkHttpClient (okhttp3.OkHttpClient)103 IOException (java.io.IOException)89 RequestBody (okhttp3.RequestBody)68 Test (org.junit.Test)68 File (java.io.File)36 MultipartBody (okhttp3.MultipartBody)36 HttpUrl (okhttp3.HttpUrl)34 Map (java.util.Map)31 MockResponse (okhttp3.mockwebserver.MockResponse)31 Call (okhttp3.Call)26 Interceptor (okhttp3.Interceptor)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)24 Builder (okhttp3.OkHttpClient.Builder)21 Retrofit (retrofit2.Retrofit)21 Builder (okhttp3.Request.Builder)19 FormBody (okhttp3.FormBody)18 ResponseBody (okhttp3.ResponseBody)18