Search in sources :

Example 1 with SerializationException

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);
    }
}
Also used : SerializationException(ly.apps.android.rest.exceptions.SerializationException) IOException(java.io.IOException)

Example 2 with SerializationException

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);
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) SerializationException(ly.apps.android.rest.exceptions.SerializationException) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Example 3 with SerializationException

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);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) SerializationException(ly.apps.android.rest.exceptions.SerializationException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Aggregations

SerializationException (ly.apps.android.rest.exceptions.SerializationException)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 StringEntity (org.apache.http.entity.StringEntity)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)1 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)1