Search in sources :

Example 81 with Builder

use of okhttp3.FormBody.Builder in project sonarqube by SonarSource.

the class WebhookCallerImpl method call.

@Override
public WebhookDelivery call(Webhook webhook, WebhookPayload payload) {
    WebhookDelivery.Builder builder = new WebhookDelivery.Builder();
    long startedAt = system.now();
    builder.setAt(startedAt).setPayload(payload).setWebhook(webhook);
    try {
        Request request = buildHttpRequest(webhook, payload);
        try (Response response = execute(request)) {
            builder.setHttpStatus(response.code());
            builder.setDurationInMs((int) (system.now() - startedAt));
        }
    } catch (Exception e) {
        builder.setError(e);
    }
    return builder.build();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException)

Example 82 with Builder

use of okhttp3.FormBody.Builder in project MVCHelper by LuckyJayce.

the class NetworkExeption method toMessage.

private final String toMessage() {
    ResponseBody body = response.body();
    StringBuilder builder = new StringBuilder("url:").append(url);
    builder.append(" code:").append(httpCode);
    builder.append(" message:").append(response.message());
    if (body != null) {
        try {
            builder.append(" body:").append(body.string());
            body.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return builder.toString();
}
Also used : IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 83 with Builder

use of okhttp3.FormBody.Builder in project MVCHelper by LuckyJayce.

the class DeleteMethod method buildRequset.

@Override
protected Request.Builder buildRequset(String url, Map<String, Object> params) {
    FormBody.Builder builder = new FormBody.Builder();
    if (params != null) {
        for (Entry<String, ?> entry : params.entrySet()) {
            builder.add(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    RequestBody formBody = builder.build();
    return new Request.Builder().url(url).delete(formBody);
}
Also used : FormBody(okhttp3.FormBody) RequestBody(okhttp3.RequestBody)

Example 84 with Builder

use of okhttp3.FormBody.Builder in project RxSample by Aload.

the class RequestManger method getRetrofit.

/**
     * 获取retrofit对象
     *
     * @param mListener
     * @return
     */
public Retrofit getRetrofit(IBaseRXListener mListener) {
    String url = mListener.getUrl();
    if (null == mRetrofit || !url.equals(mRetrofit.baseUrl().toString())) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder().readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).addInterceptor(//拦截器处理通用Header
        interceptor).addInterceptor(//日志处理
        loggingInterceptor).connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS).writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        //可以区别http,https
        if (!StringUtil.isNullOrEmpty(url) && url.toLowerCase().contains("https:")) {
            try {
                builder.sslSocketFactory(getSSLSocketFactory(new int[] { mListener.getRawSSl() }));
                builder.hostnameVerifier(new HostVerifyUtils(new String[] { url }));
            } catch (Exception e) {
                mListener.getException(e);
            }
        }
        OkHttpClient client = builder.build();
        mRetrofit = new Retrofit.Builder().client(client).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl(url).build();
    }
    return mRetrofit;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HostVerifyUtils(com.autism.rxsample.utils.rxutils.HostVerifyUtils) IOException(java.io.IOException)

Example 85 with Builder

use of okhttp3.FormBody.Builder in project Gradle-demo by Arisono.

the class LogInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Map<String, Object> headers = new HashMap<>();
    Map<String, Object> params = new HashMap<>();
    Map<String, Object> postParm = new HashMap<>();
    //添加公共Header,公共参数
    if (builder != null) {
        headers = builder.getHeaders();
        params = builder.getParams();
        if (!headers.isEmpty()) {
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                request = request.newBuilder().addHeader(entry.getKey(), String.valueOf(entry.getValue())).build();
            }
        }
        if (!params.isEmpty()) {
            //get请求    添加公共参数
            if (request.method().equals("GET")) {
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    HttpUrl httpUrl = request.url().newBuilder().addQueryParameter(entry.getKey(), String.valueOf(entry.getValue())).build();
                    request = request.newBuilder().url(httpUrl).build();
                }
            }
            if (request.method().equals("POST")) {
                if (request.body() instanceof FormBody) {
                    FormBody.Builder bodyBuilder = new FormBody.Builder();
                    FormBody formBody = (FormBody) request.body();
                    for (int i = 0; i < formBody.size(); i++) {
                        postParm.put(formBody.encodedName(i), formBody.encodedValue(i));
                        bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i));
                    }
                    for (Map.Entry<String, Object> entry : params.entrySet()) {
                        formBody = bodyBuilder.addEncoded(entry.getKey(), String.valueOf(entry.getValue())).build();
                    }
                    request = request.newBuilder().post(formBody).build();
                }
            }
        }
    }
    Response response = chain.proceed(request);
    okhttp3.MediaType mediaType = response.body().contentType();
    String content = response.body().string();
    if (true) {
        OkhttpUtils.println("|---------------日志打印  start---------------------------|");
        OkhttpUtils.println("请求头:" + JSON.toJSONString(response.request().headers().toMultimap()));
        OkhttpUtils.println("url:" + JSON.toJSONString(response.request().url().toString()));
        OkhttpUtils.println("参数:" + JSON.toJSONString(postParm));
        OkhttpUtils.println("结果:" + content);
        OkhttpUtils.println("|---------------日志打印   end---------------------------|");
    }
    return response.newBuilder().body(okhttp3.ResponseBody.create(mediaType, content)).build();
}
Also used : HashMap(java.util.HashMap) Request(okhttp3.Request) FormBody(okhttp3.FormBody) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) Map(java.util.Map) HashMap(java.util.HashMap)

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