use of retrofit2.Converter in project mobile-sdk-android by meniga.
the class MenigaTransactionUpdateConverter method responseBodyConverter.
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, final Retrofit retrofit) {
Type typeOfTransactionUpdate = new TypeToken<MenigaTransactionUpdate>() {
}.getType();
if (typeOfTransactionUpdate.equals(type)) {
return new Converter<ResponseBody, Object>() {
@Override
public Object convert(ResponseBody resBody) throws IOException {
Gson gson = GsonProvider.getGsonBuilder();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(resBody.byteStream());
JsonElement element = new JsonParser().parse(isr);
isr.close();
JsonObject json = element.getAsJsonObject();
List<MenigaTransaction> updatedTransactions = new ArrayList<>();
if (json.get("data").isJsonArray()) {
updatedTransactions = gson.fromJson(json.getAsJsonArray("data"), new TypeToken<List<MenigaTransaction>>() {
}.getType());
} else {
MenigaTransaction updated = gson.fromJson(json.getAsJsonObject("data"), new TypeToken<MenigaTransaction>() {
}.getType());
updatedTransactions.add(updated);
}
List<MenigaTransaction> transactions = new ArrayList<>();
if (json.has("meta")) {
transactions = gson.fromJson(json.getAsJsonArray("meta"), new TypeToken<List<MenigaTransaction>>() {
}.getType());
}
return MenigaTransactionUpdate.fromList(updatedTransactions, transactions);
} finally {
if (isr != null) {
isr.close();
}
}
}
};
}
return null;
}
use of retrofit2.Converter in project xabber-android by redsolution.
the class RetrofitErrorConverter method throwableToHttpError.
@Nullable
public static String throwableToHttpError(Throwable throwable) {
String errorMessage = null;
APIError error = null;
if (throwable instanceof HttpException) {
HttpException exception = (HttpException) throwable;
Response response = exception.response();
ResponseBody responseBody = response.errorBody();
if (responseBody != null) {
Converter<ResponseBody, APIError> converter = HttpApiManager.getRetrofit().responseBodyConverter(APIError.class, new Annotation[0]);
try {
error = converter.convert(responseBody);
} catch (IOException | JsonSyntaxException e) {
e.printStackTrace();
}
}
if (error != null) {
if (error.getDetail() != null)
errorMessage = error.getDetail();
else if (error.getEmail() != null && error.getEmail().size() > 0)
errorMessage = error.getEmail().get(0);
else if (error.getCredentials() != null && error.getCredentials().size() > 0)
errorMessage = error.getCredentials().get(0);
else if (error.getCode() != null && error.getCode().size() > 0)
errorMessage = error.getCode().get(0);
else if (error.getUsername() != null && error.getUsername().size() > 0)
errorMessage = error.getUsername().get(0);
else if (error.getPhone() != null && error.getPhone().size() > 0)
errorMessage = error.getPhone().get(0);
}
}
return errorMessage;
}
use of retrofit2.Converter in project retrofit by square.
the class CallTest method conversionProblemIncomingMaskedByConverterIsUnwrapped.
@Test
public void conversionProblemIncomingMaskedByConverterIsUnwrapped() throws IOException {
// MWS has no way to trigger IOExceptions during the response body so use an interceptor.
OkHttpClient client = //
new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response response = chain.proceed(chain.request());
ResponseBody body = response.body();
BufferedSource source = Okio.buffer(new ForwardingSource(body.source()) {
@Override
public long read(Buffer sink, long byteCount) throws IOException {
throw new IOException("cause");
}
});
body = ResponseBody.create(body.contentType(), body.contentLength(), source);
return response.newBuilder().body(body).build();
}
}).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).client(client).addConverterFactory(new ToStringConverterFactory() {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
try {
return value.string();
} catch (IOException e) {
// Some serialization libraries mask transport problems in runtime exceptions. Bad!
throw new RuntimeException("wrapper", e);
}
}
};
}
}).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
Call<String> call = example.getString();
try {
call.execute();
fail();
} catch (IOException e) {
assertThat(e).hasMessage("cause");
}
}
use of retrofit2.Converter in project retrofit by square.
the class CallTest method conversionProblemOutgoingAsync.
@Test
public void conversionProblemOutgoingAsync() throws InterruptedException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory() {
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return new Converter<String, RequestBody>() {
@Override
public RequestBody convert(String value) throws IOException {
throw new UnsupportedOperationException("I am broken!");
}
};
}
}).build();
Service example = retrofit.create(Service.class);
final AtomicReference<Throwable> failureRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
example.postString("Hi").enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
throw new AssertionError();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
failureRef.set(t);
latch.countDown();
}
});
assertTrue(latch.await(10, SECONDS));
assertThat(failureRef.get()).isInstanceOf(UnsupportedOperationException.class).hasMessage("I am broken!");
}
use of retrofit2.Converter in project retrofit by square.
the class CallTest method conversionProblemIncomingAsync.
@Test
public void conversionProblemIncomingAsync() throws InterruptedException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory() {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
throw new UnsupportedOperationException("I am broken!");
}
};
}
}).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
final AtomicReference<Throwable> failureRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
example.postString("Hi").enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
throw new AssertionError();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
failureRef.set(t);
latch.countDown();
}
});
assertTrue(latch.await(10, SECONDS));
assertThat(failureRef.get()).isInstanceOf(UnsupportedOperationException.class).hasMessage("I am broken!");
}
Aggregations