Search in sources :

Example 1 with HttpEntityWrapper

use of org.apache.http.entity.HttpEntityWrapper in project SmartAndroidSource by jaychou2012.

the class AsyncHttpClient method endEntityViaReflection.

/**
	 * This horrible hack is required on Android, due to implementation of
	 * BasicManagedEntity, which doesn't chain call consumeContent on underlying
	 * wrapped HttpEntity
	 * 
	 * @param entity
	 *            HttpEntity, may be null
	 */
public static void endEntityViaReflection(HttpEntity entity) {
    if (entity instanceof HttpEntityWrapper) {
        try {
            Field f = null;
            Field[] fields = HttpEntityWrapper.class.getDeclaredFields();
            for (Field ff : fields) {
                if (ff.getName().equals("wrappedEntity")) {
                    f = ff;
                    break;
                }
            }
            if (f != null) {
                f.setAccessible(true);
                HttpEntity wrapped = (HttpEntity) f.get(entity);
                if (wrapped != null) {
                    wrapped.consumeContent();
                }
            }
        } catch (Throwable t) {
            Log.e(LOG_TAG, "wrappedEntity consume", t);
        }
    }
}
Also used : Field(java.lang.reflect.Field) HttpEntityWrapper(org.apache.http.entity.HttpEntityWrapper) HttpEntity(org.apache.http.HttpEntity)

Example 2 with HttpEntityWrapper

use of org.apache.http.entity.HttpEntityWrapper in project robolectric by robolectric.

the class ShadowDefaultRequestDirector method interceptResponseContent.

private void interceptResponseContent(HttpResponse response) {
    HttpEntity entity = response.getEntity();
    if (entity instanceof HttpEntityWrapper) {
        HttpEntityWrapper entityWrapper = (HttpEntityWrapper) entity;
        try {
            Field wrappedEntity = HttpEntityWrapper.class.getDeclaredField("wrappedEntity");
            wrappedEntity.setAccessible(true);
            entity = (HttpEntity) wrappedEntity.get(entityWrapper);
        } catch (Exception e) {
        // fail to record
        }
    }
    if (entity instanceof BasicHttpEntity) {
        BasicHttpEntity basicEntity = (BasicHttpEntity) entity;
        try {
            Field contentField = BasicHttpEntity.class.getDeclaredField("content");
            contentField.setAccessible(true);
            InputStream content = (InputStream) contentField.get(basicEntity);
            byte[] buffer = Util.readBytes(content);
            FakeHttp.getFakeHttpLayer().addHttpResponseContent(buffer);
            contentField.set(basicEntity, new ByteArrayInputStream(buffer));
        } catch (Exception e) {
        // fail to record
        }
    }
}
Also used : Field(java.lang.reflect.Field) HttpEntity(org.apache.http.HttpEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpEntityWrapper(org.apache.http.entity.HttpEntityWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException) HttpException(org.apache.http.HttpException)

Aggregations

Field (java.lang.reflect.Field)2 HttpEntity (org.apache.http.HttpEntity)2 HttpEntityWrapper (org.apache.http.entity.HttpEntityWrapper)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpException (org.apache.http.HttpException)1 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)1