Search in sources :

Example 56 with Builder

use of okhttp3.HttpUrl.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 57 with Builder

use of okhttp3.HttpUrl.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 58 with Builder

use of okhttp3.HttpUrl.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 59 with Builder

use of okhttp3.HttpUrl.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 60 with Builder

use of okhttp3.HttpUrl.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)206 Response (okhttp3.Response)148 OkHttpClient (okhttp3.OkHttpClient)142 IOException (java.io.IOException)111 RequestBody (okhttp3.RequestBody)81 Test (org.junit.Test)75 HttpUrl (okhttp3.HttpUrl)47 File (java.io.File)42 MockResponse (okhttp3.mockwebserver.MockResponse)42 MultipartBody (okhttp3.MultipartBody)40 Map (java.util.Map)39 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)31 Call (okhttp3.Call)29 Interceptor (okhttp3.Interceptor)29 Retrofit (retrofit2.Retrofit)29 Builder (okhttp3.OkHttpClient.Builder)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 ResponseBody (okhttp3.ResponseBody)24 HashMap (java.util.HashMap)22 FormBody (okhttp3.FormBody)21