Search in sources :

Example 51 with Builder

use of okhttp3.Request.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 52 with Builder

use of okhttp3.Request.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 53 with Builder

use of okhttp3.Request.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)

Example 54 with Builder

use of okhttp3.Request.Builder in project PocketMaps by junjunguo.

the class HttpConnection method getOkHttpClient.

private static OkHttpClient getOkHttpClient() {
    if (client == null) {
        Builder b = new Builder();
        b.connectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
        b.readTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
        client = b.build();
    /*
            client.setConnectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
            client.setReadTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
            */
    }
    return client;
}
Also used : Builder(okhttp3.OkHttpClient.Builder)

Example 55 with Builder

use of okhttp3.Request.Builder in project android-diplicity by zond.

the class RetrofitActivity method recreateServices.

protected void recreateServices() {
    AuthenticatingCallAdapterFactory adapterFactory = new AuthenticatingCallAdapterFactory();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request toIssue = chain.request().newBuilder().addHeader("Accept", "application/json; charset=UTF-8").addHeader("X-Diplicity-API-Level", "" + DIPLICITY_API_LEVEL).build();
            if (getLocalDevelopmentMode() && !getLocalDevelopmentModeFakeID().equals("")) {
                HttpUrl url = toIssue.url().newBuilder().addQueryParameter("fake-id", getLocalDevelopmentModeFakeID()).build();
                toIssue = toIssue.newBuilder().url(url).build();
            } else if (!getAuthToken().equals("")) {
                toIssue = toIssue.newBuilder().addHeader("Authorization", "bearer " + getAuthToken()).build();
            }
            Log.d("Diplicity", "" + toIssue.method() + "ing " + toIssue.url());
            return chain.proceed(toIssue);
        }
    });
    builder.connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS);
    Gson gson = new GsonBuilder().registerTypeAdapter(Ticker.class, new TickerUnserializer()).registerTypeAdapter(Game.class, new GameUnserializer(this)).create();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseURL()).addConverterFactory(GsonConverterFactory.create(gson)).addCallAdapterFactory(adapterFactory).client(builder.build()).build();
    gameService = retrofit.create(GameService.class);
    userStatsService = retrofit.create(UserStatsService.class);
    memberService = retrofit.create(MemberService.class);
    rootService = retrofit.create(RootService.class);
    variantService = retrofit.create(VariantService.class);
    optionsService = retrofit.create(OptionsService.class);
    orderService = retrofit.create(OrderService.class);
    phaseService = retrofit.create(PhaseService.class);
    channelService = retrofit.create(ChannelService.class);
    messageService = retrofit.create(MessageService.class);
    phaseResultService = retrofit.create(PhaseResultService.class);
    gameResultService = retrofit.create(GameResultService.class);
    phaseStateService = retrofit.create(PhaseStateService.class);
    gameStateService = retrofit.create(GameStateService.class);
    userConfigService = retrofit.create(UserConfigService.class);
    banService = retrofit.create(BanService.class);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) PhaseService(se.oort.diplicity.apigen.PhaseService) GameStateService(se.oort.diplicity.apigen.GameStateService) PhaseResultService(se.oort.diplicity.apigen.PhaseResultService) BanService(se.oort.diplicity.apigen.BanService) Interceptor(okhttp3.Interceptor) GsonBuilder(com.google.gson.GsonBuilder) MemberService(se.oort.diplicity.apigen.MemberService) Request(okhttp3.Request) UserConfigService(se.oort.diplicity.apigen.UserConfigService) IOException(java.io.IOException) TickerUnserializer(se.oort.diplicity.apigen.TickerUnserializer) GameResultService(se.oort.diplicity.apigen.GameResultService) UserStatsService(se.oort.diplicity.apigen.UserStatsService) HttpUrl(okhttp3.HttpUrl) MessageService(se.oort.diplicity.apigen.MessageService) PhaseStateService(se.oort.diplicity.apigen.PhaseStateService) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Game(se.oort.diplicity.apigen.Game) GameService(se.oort.diplicity.apigen.GameService) OrderService(se.oort.diplicity.apigen.OrderService)

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