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);
}
}
}
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
}
}
}
Aggregations