use of okhttp3 in project retrofit by square.
the class RetrofitTest method callFactoryThrowingPropagates.
@Test
public void callFactoryThrowingPropagates() {
final RuntimeException cause = new RuntimeException("Broken!");
okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {
@Override
public okhttp3.Call newCall(Request request) {
throw cause;
}
};
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").callFactory(callFactory).build();
server.enqueue(new MockResponse());
CallMethod service = retrofit.create(CallMethod.class);
Call<ResponseBody> call = service.getResponseBody();
try {
call.execute();
fail();
} catch (Exception e) {
assertThat(e).isSameAs(cause);
}
}
use of okhttp3 in project retrofit by square.
the class RetrofitTest method callFactoryReturningNullThrows.
@Test
public void callFactoryReturningNullThrows() throws IOException {
okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {
@Override
public okhttp3.Call newCall(Request request) {
return null;
}
};
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").callFactory(callFactory).build();
server.enqueue(new MockResponse());
CallMethod service = retrofit.create(CallMethod.class);
Call<ResponseBody> call = service.getResponseBody();
try {
call.execute();
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessage("Call.Factory returned null.");
}
}
use of okhttp3 in project sonarqube by SonarSource.
the class WebhookCallerImpl method followPostRedirect.
/**
* Inspired by https://github.com/square/okhttp/blob/parent-3.6.0/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java#L286
*/
private Response followPostRedirect(Response response) throws IOException {
String location = response.header("Location");
if (location == null) {
throw new IllegalStateException(format("Missing HTTP header 'Location' in redirect of %s", response.request().url()));
}
HttpUrl url = response.request().url().resolve(location);
// Don't follow redirects to unsupported protocols.
if (url == null) {
throw new IllegalStateException(format("Unsupported protocol in redirect of %s to %s", response.request().url(), location));
}
Request.Builder redirectRequest = response.request().newBuilder();
redirectRequest.post(response.request().body());
response.body().close();
return okHttpClient.newCall(redirectRequest.url(url).build()).execute();
}
use of okhttp3 in project WordPress-Android by wordpress-mobile.
the class GravatarApi method uploadGravatar.
public static void uploadGravatar(final File file, final String email, final String accessToken, final GravatarUploadListener gravatarUploadListener) {
Request request = prepareGravatarUpload(email, file);
createClient(accessToken).newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
Map<String, Object> properties = new HashMap<>();
properties.put("network_response_code", response.code());
// response's body can only be read once so, keep it in a local variable
String responseBody;
try {
responseBody = response.body().string();
} catch (IOException e) {
responseBody = "null";
}
properties.put("network_response_body", responseBody);
AnalyticsTracker.track(AnalyticsTracker.Stat.ME_GRAVATAR_UPLOAD_UNSUCCESSFUL, properties);
AppLog.w(AppLog.T.API, "Network call unsuccessful trying to upload Gravatar: " + responseBody);
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (response.isSuccessful()) {
gravatarUploadListener.onSuccess();
} else {
gravatarUploadListener.onError();
}
}
});
}
@Override
public void onFailure(okhttp3.Call call, final IOException e) {
Map<String, Object> properties = new HashMap<>();
properties.put("network_exception_class", e != null ? e.getClass().getCanonicalName() : "null");
properties.put("network_exception_message", e != null ? e.getMessage() : "null");
AnalyticsTracker.track(AnalyticsTracker.Stat.ME_GRAVATAR_UPLOAD_EXCEPTION, properties);
CrashlyticsUtils.logException(e, AppLog.T.API, "Network call failure trying to upload Gravatar!");
AppLog.w(AppLog.T.API, "Network call failure trying to upload Gravatar!" + (e != null ? e.getMessage() : "null"));
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
gravatarUploadListener.onError();
}
});
}
});
}
use of okhttp3 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