Search in sources :

Example 26 with Builder

use of okhttp3.FormBody.Builder in project YhLibraryForAndroid by android-coco.

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(org.yh.library.okhttp.builder.PostFormBuilder) FormBody(okhttp3.FormBody) PostFormBuilder(org.yh.library.okhttp.builder.PostFormBuilder) RequestBody(okhttp3.RequestBody)

Example 27 with Builder

use of okhttp3.FormBody.Builder in project LabDayApp by jakdor.

the class AuthenticationInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request original = chain.request();
    Request.Builder builder = original.newBuilder().header("Authorization", "token " + authToken);
    Request request = builder.build();
    return chain.proceed(request);
}
Also used : Request(okhttp3.Request)

Example 28 with Builder

use of okhttp3.FormBody.Builder in project hub-fortify-ssc-integration-service by blackducksoftware.

the class FortifyService method getHeader.

public static Builder getHeader(String userName, String password) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(Level.BASIC);
    OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
    okBuilder.authenticator(new Authenticator() {

        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            String credential = Credentials.basic(userName, password);
            if (credential.equals(response.request().header("Authorization"))) {
                try {
                    FortifyExceptionUtil.verifyFortifyResponseCode(response.code(), "Unauthorized access of Fortify Api");
                } catch (IntegrationException e) {
                    throw new IOException(e);
                }
                return null;
            }
            return response.request().newBuilder().header("Authorization", credential).build();
        }
    });
    okBuilder.addInterceptor(logging);
    return okBuilder;
}
Also used : Builder(okhttp3.OkHttpClient.Builder) Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) Builder(okhttp3.OkHttpClient.Builder) Request(okhttp3.Request) IOException(java.io.IOException) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Example 29 with Builder

use of okhttp3.FormBody.Builder in project BaseProject by fly803.

the class RetrofitRequestManager method genericClient.

public static OkHttpClient genericClient() {
    /**
     * 类型为BASIC,其实日志级别分为4类:NONE、BASIC、HEADERS、BODY。
     *         大家看下我打印出来的日志,就知道这4类的区别了。
     *         1、NONE
     *         没有任何log
     *         2、BASIC
     *         请求/响应行
     *         basic的格式:
     *         --> POST 地址 http/1.1 (0-byte body)
     *         <-- 200 OK 地址 (154ms, unknown-length body)
     *         3、HEADERS
     *         请求/响应行 + 头
     *         4、BODY
     *         请求/响应行 + 头 + 体
     */
    final OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(15, TimeUnit.SECONDS);
    builder.readTimeout(20, TimeUnit.SECONDS);
    builder.writeTimeout(20, TimeUnit.SECONDS);
    // 错误重连
    builder.retryOnConnectionFailure(true);
    // 新建log拦截器
    final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

        @Override
        public void log(String message) {
            Log.w(BaseProjectConfig.TAG, "genericClient log: " + message);
        }
    });
    HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BASIC;
    loggingInterceptor.setLevel(level);
    if (BaseProjectConfig.DEBUG) {
        // 添加retrofit日志打印 }
        builder.addInterceptor(loggingInterceptor);
        // builder.addInterceptor(new HeaderInterceptor());
        builder.addInterceptor(new BaseUrlInterceptor());
    }
    OkHttpClient httpClient = builder.build();
    return httpClient;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BaseUrlInterceptor(com.cg.baseproject.request.retrofit.interceptor.BaseUrlInterceptor)

Example 30 with Builder

use of okhttp3.FormBody.Builder in project AndroidComponent by funnyzhaov.

the class HeaderInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request originalrequest = chain.request();
    Request.Builder builder = originalrequest.newBuilder().header("xx", "xx").method(originalrequest.method(), originalrequest.body());
    Request request = builder.build();
    return chain.proceed(request);
}
Also used : Request(okhttp3.Request)

Aggregations

Request (okhttp3.Request)190 Response (okhttp3.Response)133 OkHttpClient (okhttp3.OkHttpClient)128 IOException (java.io.IOException)100 RequestBody (okhttp3.RequestBody)77 Test (org.junit.Test)68 File (java.io.File)42 HttpUrl (okhttp3.HttpUrl)40 MultipartBody (okhttp3.MultipartBody)40 Map (java.util.Map)34 MockResponse (okhttp3.mockwebserver.MockResponse)32 Call (okhttp3.Call)29 Retrofit (retrofit2.Retrofit)29 Interceptor (okhttp3.Interceptor)28 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 Builder (okhttp3.OkHttpClient.Builder)24 ResponseBody (okhttp3.ResponseBody)21 Provides (dagger.Provides)20 FormBody (okhttp3.FormBody)20