0

I'm dealing with problems (memory leak, ui freeze, ...) using loopj (latest version: 1.4.6) to download some large (5m+) text data (complex json encoded objects which contain base64-encoded binary data as one of their fields). here's the base class which extends TextHttpResponseHandler to download data

public class AsyncResponseHandler extends TextHttpResponseHandler {

    private final HttpAsyncResultTask asyncResultTask;

    private final Logger logger = LoggerFactory.getLogger(AsyncResponseHandler.class);

    public AsyncResponseHandler(final HttpAsyncResultTask asyncResultTask){
        super("utf-8"); // todo: ...
        this.asyncResultTask = asyncResultTask;
    }


    @Override
    public void onFailure(final int i, final Header[] headers, final String s, final Throwable throwable) {
        logger.warn("got failure: i: " + i + " , s: " + s + " , t: " + throwable);
        asyncResultTask.onFailure();
    }

    @Override
    public void onSuccess(final int i, final Header[] headers, final String s) {
        logger.debug("got success: i: " + i + " , s: " + s);
        asyncResultTask.onSuccess(s);

    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBytes) {
        super.onSuccess(statusCode, headers, responseBytes);
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
        super.onFailure(statusCode, headers, responseBytes, throwable);
    }

}

note that, the HttpAsyncResultTask interface might have multiple implementations based on the given task

Edited well, I changed the approach a little bit by downloading the smaller file chunks of the resource separately (& concurrently); so, now the size of the resource is not the issue. but I'm still getting ANR in my app and with the help of logs, I'm pretty sure that the ANR happens when the application tries to make http calls. these http calls is created within the background service and according to the loopj documentation, it should be async. any ideas on the reason of the ANRs ?

magpie
  • 41
  • 1
  • 7

1 Answers1

0

The problem here is the lib, it uses Apache Http Client to download all server data, and it has bugs, the Android team says "use HttpURLConnection instead".

I relly recommend you use OkHttpClient instead of AsyncHttpClient.

But parse a base64 binary on a text response is not a good think.

Leonardo
  • 155
  • 3
  • seriously ? are u sure ? I thought it was stable enough ( based on the big projects which has been using the library, e.g instagram, pinterest , etc ) – magpie Jan 13 '15 at 08:17
  • and about the base64 encoding, I thought it's the only way when u're dealing with the complex object which has various fields (text, boolean, binary); what I did was encode the binary fields with base64 and present the whole data structure with json; any other suggestions ? – magpie Jan 13 '15 at 08:21
  • Normally when I do this I work with links, so send the object normally and on a binary field we send a link, and on a second request download the file. – Leonardo Jan 13 '15 at 16:00