3

I have build an app where I used Okhttp and retrofit. Everything works fine except some irregular wired behaviour. In this news app I trigger feed download request for multiple sections. Time to time requests never return any response (not even throw exceptions) it just get stuck and in Logcat I see continuous garbage collections.

I tried to find potential suspect find can't find anything. Only thing I can see from "Threads" in ddms is that lots of Threads are in "monitor" state (please see the screenshot)

enter image description here

Edit: Another usual Thread dump for retrofit:

enter image description here

Here is the Retrofit config:

public interface ApiService {
    @GET("/feed/get-news-feed/{section}")
    void getNewsArticles(
            @Path("section") String section, 
            Callback<GeneratedNewsEntryList> callback
    );
}

public static ApiService getInstance() {
    if(service == null) {
        OkHttpClient okHttpClient = new OkHttpClient();
        File cacheDir = App.getContext().getCacheDir();
        HttpResponseCache cache = null;
        try {
            cache = new HttpResponseCache(cacheDir, 1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
        okHttpClient.setResponseCache(cache);

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setServer(BASE_URL)
                .setClient(new OkClient(okHttpClient))
                .setErrorHandler(new ErrorHandler() {

                    @Override
                    public Throwable handleError(RetrofitError arg0) {
                        if(arg0.getResponse().getStatus() == 404)
                            return new Exception("Url does not exists");

                        return new Exception(arg0.getMessage());
                    }
                })
                .build();
        service = restAdapter.create(ApiService.class);
    }

    return service;
}

Any idea, anyone experience similar issues?

Mohammad Haque
  • 565
  • 1
  • 7
  • 16
  • Hey I am having issues using this, was wondering if you can just help me out... was wondering where you got service from in his example? Can you show more code please? thanks – Lion789 Mar 17 '14 at 08:21
  • Maybe you can help answer this question http://stackoverflow.com/questions/22445177/trying-to-make-use-of-httpcache-android – Lion789 Mar 17 '14 at 08:53

1 Answers1

0

Looks like either a bug in the response cache, or a file system that is not working very well.

The response cache holds a lock while writing files to the file system, and it looks like one of the writes is not completing very quickly. Can you show the thread dumps for the other threads?

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Sorry, don't know what other Thread dump you are referring to: I added another screenshot in my post, not sure it will help or not? – Mohammad Haque Jan 13 '14 at 00:13
  • Just a bit more info: I have six sections for news and on app startup it triggers six GET request in sequentially (just execute six request one after another, not waiting for receiving response of last request before sending next request). Could it be related? – Mohammad Haque Jan 13 '14 at 00:22
  • I have removed the cache folder setup for okHttp. After that I did not experience this issues. File cacheDir = App.getContext().getCacheDir(); okHttpClient.setResponseCache(cache); – Mohammad Haque Jan 27 '14 at 16:16
  • Good to hear Mohammad. If you see something again, please file a bug on the OkHttp issue tracker and I'll take a look. – Jesse Wilson Jan 29 '14 at 02:43