use of ly.apps.android.rest.exceptions.SerializationException in project appsly-android-rest by 47deg.
the class JacksonBodyConverter method fromResponseBody.
@Override
@SuppressWarnings("unchecked")
public <T> T fromResponseBody(Type target, String contentType, HttpEntity responseBody, CacheAwareCallback<T> callback) {
Logger.d("JacksonBodyConverter.fromResponseBody: target: " + target + " responseBody: " + responseBody);
try {
T result;
if (callback.isResponseIsCollection()) {
result = mapper.readValue(responseBody.getContent(), mapper.getTypeFactory().constructCollectionType(callback.getCollectionType(), (Class<T>) target));
} else {
result = mapper.readValue(responseBody.getContent(), (Class<T>) target);
}
Logger.d("JacksonBodyConverter.fromResponseBody: result: " + result);
return result;
} catch (IOException e) {
throw new SerializationException(e);
}
}
use of ly.apps.android.rest.exceptions.SerializationException in project appsly-android-rest by 47deg.
the class JacksonHttpFormValuesConverter method toRequestBody.
@Override
@SuppressWarnings("unchecked")
public <T> HttpEntity toRequestBody(T object, String contentType) {
Logger.d("JacksonHttpFormValuesConverter.toRequestBody: object: " + object);
try {
HttpEntity entity = null;
List<NameValuePair> vals = new ArrayList<NameValuePair>();
if (HeaderUtils.CONTENT_TYPE_FORM_URL_ENCODED.startsWith(contentType)) {
Map<String, Object> props = mapper.convertValue(object, Map.class);
for (Map.Entry<String, Object> formPartEntry : props.entrySet()) {
if (formPartEntry.getValue() != null) {
vals.add(new BasicNameValuePair(formPartEntry.getKey(), formPartEntry.getValue().toString()));
}
}
entity = new UrlEncodedFormEntity(vals);
} else if (HeaderUtils.CONTENT_TYPE_MULTIPART_FORM_DATA.startsWith(contentType)) {
Map<String, Object> props = mapper.convertValue(object, Map.class);
MultipartEntity multipartEntity = new MultipartEntity(null);
for (Map.Entry<String, Object> formPartEntry : props.entrySet()) {
if (formPartEntry.getValue() != null) {
if (formPartEntry.getValue() instanceof FileFormField) {
FileFormField fileFormField = (FileFormField) formPartEntry.getValue();
multipartEntity.addPart(formPartEntry.getKey(), fileFormField.getFile(), fileFormField.getContentType());
} else {
multipartEntity.addPart(formPartEntry.getKey(), formPartEntry.getValue().toString());
}
}
}
entity = multipartEntity;
}
return entity;
} catch (UnsupportedEncodingException e) {
throw new SerializationException(e);
}
}
use of ly.apps.android.rest.exceptions.SerializationException in project appsly-android-rest by 47deg.
the class JacksonBodyConverter method toRequestBody.
@Override
public <T> HttpEntity toRequestBody(T object, String contentType) {
Logger.d("JacksonBodyConverter.toRequestBody: object: " + object);
try {
String json = mapper.writeValueAsString(object);
Logger.d("JacksonHttpFormValuesConverter.toRequestBody: json: " + json);
StringEntity result = new StringEntity(json, "UTF-8");
result.setContentType(contentType);
Logger.d("JacksonBodyConverter.toRequestBody: result: " + result);
return result;
} catch (UnsupportedEncodingException e) {
throw new SerializationException(e);
} catch (JsonMappingException e) {
throw new SerializationException(e);
} catch (JsonGenerationException e) {
throw new SerializationException(e);
} catch (IOException e) {
throw new SerializationException(e);
}
}
Aggregations