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();
}
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();
}
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);
}
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;
}
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();
}
Aggregations