Search in sources :

Example 56 with Builder

use of okhttp3.OkHttpClient.Builder in project RxJava-Android-Samples by kaushikgopal.

the class GithubService method createGithubService.

public static GithubApi createGithubService(final String githubToken) {
    Retrofit.Builder builder = new Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).baseUrl("https://api.github.com");
    if (!TextUtils.isEmpty(githubToken)) {
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(chain -> {
            Request request = chain.request();
            Request newReq = request.newBuilder().addHeader("Authorization", format("token %s", githubToken)).build();
            return chain.proceed(newReq);
        }).build();
        builder.client(client);
    }
    return builder.build().create(GithubApi.class);
}
Also used : RxJava2CallAdapterFactory(com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory) Request(okhttp3.Request) OkHttpClient(okhttp3.OkHttpClient) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) TextUtils(android.text.TextUtils) Retrofit(retrofit2.Retrofit) String.format(java.lang.String.format) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request)

Example 57 with Builder

use of okhttp3.OkHttpClient.Builder in project okhttputils by hongyangAndroid.

the class PostFormRequest method buildRequestBody.

@Override
protected RequestBody buildRequestBody() {
    if (files == null || files.isEmpty()) {
        FormBody.Builder builder = new FormBody.Builder();
        addParams(builder);
        FormBody formBody = builder.build();
        return formBody;
    } else {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        addParams(builder);
        for (int i = 0; i < files.size(); i++) {
            PostFormBuilder.FileInput fileInput = files.get(i);
            RequestBody fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileInput.filename)), fileInput.file);
            builder.addFormDataPart(fileInput.key, fileInput.filename, fileBody);
        }
        return builder.build();
    }
}
Also used : MultipartBody(okhttp3.MultipartBody) PostFormBuilder(com.zhy.http.okhttp.builder.PostFormBuilder) FormBody(okhttp3.FormBody) PostFormBuilder(com.zhy.http.okhttp.builder.PostFormBuilder) RequestBody(okhttp3.RequestBody)

Example 58 with Builder

use of okhttp3.OkHttpClient.Builder in project Fast-Android-Networking by amitshekhariitbhu.

the class RxInternalNetworking method generateSimpleObservable.

public static <T> Observable<T> generateSimpleObservable(RxANRequest request) {
    Request okHttpRequest;
    Request.Builder builder = new Request.Builder().url(request.getUrl());
    InternalNetworking.addHeadersToRequestBuilder(builder, request);
    RequestBody requestBody;
    switch(request.getMethod()) {
        case GET:
            {
                builder = builder.get();
                break;
            }
        case POST:
            {
                requestBody = request.getRequestBody();
                builder = builder.post(requestBody);
                break;
            }
        case PUT:
            {
                requestBody = request.getRequestBody();
                builder = builder.put(requestBody);
                break;
            }
        case DELETE:
            {
                requestBody = request.getRequestBody();
                builder = builder.delete(requestBody);
                break;
            }
        case HEAD:
            {
                builder = builder.head();
                break;
            }
        case PATCH:
            {
                requestBody = request.getRequestBody();
                builder = builder.patch(requestBody);
                break;
            }
    }
    if (request.getCacheControl() != null) {
        builder.cacheControl(request.getCacheControl());
    }
    okHttpRequest = builder.build();
    if (request.getOkHttpClient() != null) {
        request.setCall(request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).build().newCall(okHttpRequest));
    } else {
        request.setCall(InternalNetworking.sHttpClient.newCall(okHttpRequest));
    }
    return Observable.create(new ANOnSubscribe<T>(request));
}
Also used : GET(com.androidnetworking.common.Method.GET) PUT(com.androidnetworking.common.Method.PUT) POST(com.androidnetworking.common.Method.POST) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Example 59 with Builder

use of okhttp3.OkHttpClient.Builder in project Fast-Android-Networking by amitshekhariitbhu.

the class InternalNetworking method performUploadRequest.

public static Response performUploadRequest(ANRequest request) throws ANError {
    Request okHttpRequest;
    Response okHttpResponse;
    try {
        Request.Builder builder = new Request.Builder().url(request.getUrl());
        addHeadersToRequestBuilder(builder, request);
        final RequestBody requestBody = request.getMultiPartRequestBody();
        final long requestBodyLength = requestBody.contentLength();
        builder = builder.post(new RequestProgressBody(requestBody, request.getUploadProgressListener()));
        if (request.getCacheControl() != null) {
            builder.cacheControl(request.getCacheControl());
        }
        okHttpRequest = builder.build();
        if (request.getOkHttpClient() != null) {
            request.setCall(request.getOkHttpClient().newBuilder().cache(sHttpClient.cache()).build().newCall(okHttpRequest));
        } else {
            request.setCall(sHttpClient.newCall(okHttpRequest));
        }
        final long startTime = System.currentTimeMillis();
        okHttpResponse = request.getCall().execute();
        final long timeTaken = System.currentTimeMillis() - startTime;
        if (request.getAnalyticsListener() != null) {
            if (okHttpResponse.cacheResponse() == null) {
                Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, requestBodyLength, okHttpResponse.body().contentLength(), false);
            } else {
                if (okHttpResponse.networkResponse() == null) {
                    Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, 0, 0, true);
                } else {
                    Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, requestBodyLength != 0 ? requestBodyLength : -1, 0, true);
                }
            }
        }
    } catch (IOException ioe) {
        throw new ANError(ioe);
    }
    return okHttpResponse;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) ANRequest(com.androidnetworking.common.ANRequest) IOException(java.io.IOException) ANError(com.androidnetworking.error.ANError) RequestBody(okhttp3.RequestBody)

Example 60 with Builder

use of okhttp3.OkHttpClient.Builder in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2InternalNetworking method generateDownloadObservable.

public static <T> Observable<T> generateDownloadObservable(final Rx2ANRequest request) {
    Request okHttpRequest;
    Request.Builder builder = new Request.Builder().url(request.getUrl());
    InternalNetworking.addHeadersToRequestBuilder(builder, request);
    builder = builder.get();
    if (request.getCacheControl() != null) {
        builder.cacheControl(request.getCacheControl());
    }
    okHttpRequest = builder.build();
    OkHttpClient okHttpClient;
    if (request.getOkHttpClient() != null) {
        okHttpClient = request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).addNetworkInterceptor(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
            }
        }).build();
    } else {
        okHttpClient = InternalNetworking.sHttpClient.newBuilder().addNetworkInterceptor(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
            }
        }).build();
    }
    request.setCall(okHttpClient.newCall(okHttpRequest));
    return new DownloadANObservable<>(request);
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) ResponseProgressBody(com.androidnetworking.internal.ResponseProgressBody) Interceptor(okhttp3.Interceptor)

Aggregations

Request (okhttp3.Request)47 Response (okhttp3.Response)44 OkHttpClient (okhttp3.OkHttpClient)31 IOException (java.io.IOException)28 RequestBody (okhttp3.RequestBody)27 Test (org.junit.Test)17 Interceptor (okhttp3.Interceptor)11 File (java.io.File)9 Provides (dagger.Provides)8 X509TrustManager (javax.net.ssl.X509TrustManager)8 Retrofit (retrofit2.Retrofit)8 URI (java.net.URI)7 Map (java.util.Map)7 MultipartBody (okhttp3.MultipartBody)7 Singleton (javax.inject.Singleton)6 HttpUrl (okhttp3.HttpUrl)6 ResponseBody (okhttp3.ResponseBody)6 SSLContext (javax.net.ssl.SSLContext)5 TrustManager (javax.net.ssl.TrustManager)5 TestClients.clientRequest (keywhiz.TestClients.clientRequest)5