3

I am loading server data in my app in endless scroll recyclerView using pagination with retrofit 2 library. But unfortunately after scrolling some page retrofit onFailure method throw a message showing "Unexpected end of stream". This message shows in arbitrary pages. sometimes it shows after scrolling 6 page and sometimes 12 page.And then retrofit doesn't loading anything.

can anyone give some suggestions that work?

Kabir
  • 824
  • 4
  • 11
  • this is actually happening in the emulator but not in the real device ! may be the emulator can't hold HTTP connection for so long. so please test in a real device and make sure you add @Headers({"Connection: close"}) in each API interface. – Partho Bhowmick Mar 31 '20 at 10:32

3 Answers3

0

you can try

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();
KIRAN CSN
  • 101
  • 9
0

I have faced this issue very recently and a combination of the following helped me

  1. For the OkHttpClient, set retry on connection failure to true

    OkHttpClient client = new OkHttpClient.Builder()
      .retryOnConnectionFailure(true)
      .build(); 
    
  2. Since this is caused by the connection not closing properly, add a Connection header to your retrofit request

    @Headers({
            "Connection: close"
    })
    @FormUrlEncoded
    @POST(Const.CHAT_LIST_API)
    Call<ResponseModel<ChatListModel>> chatList(
            @Field("user_id") int userid,
            @Field("device_type") int device_type,
            @Field("token") String token,
            @Field("language") String language,
            @Field("timezone") String timezone
    );
    

The above is the interface method that will be used to call the service. you need to add the headers part of the above to your services

Retry will basically reduce the chances and the actual solution is by the header

RobC
  • 16,905
  • 14
  • 51
  • 62
Kushan
  • 5,275
  • 2
  • 27
  • 41
  • can you please elaborate or some link that explains detailed? Unfortunately i don't understand it and i am not sure where to add this chunk of code. Sorry for inconvenience. – Partho Bhowmick Aug 24 '19 at 06:21
  • You put this code where you write the interface which retrofit will use... Put it above the method declared there.... As for the okhttp search how to set custom client for retrofit, you'll get what i all saying – Kushan Aug 25 '19 at 07:41
  • I did so far. but this approach is not solving the problem yet.. can you please check the code please which is attached as a link. https://paste.ubuntu.com/p/hjFMxqx7VX/ – Partho Bhowmick Aug 25 '19 at 07:52
  • If even this is not working please check your server side response it might be giving wrong content length in the http response headers, your client code seems fine – Kushan Aug 25 '19 at 07:57
  • but the server response sometimes works fine and sometimes throws "unexpected end of stream". like when I get this onFailure() on a page then if i go back to the previous page and then immediately come to that page (which throws an exception) , then it is working properly. – Partho Bhowmick Aug 25 '19 at 08:03
0

Just add the header to your request

@Headers({"Connection: close"})

This means that you should close your connection after every request.

Manoj Perumarath
  • 6,251
  • 4
  • 39
  • 57