I have faced this issue very recently and a combination of the following helped me
For the OkHttpClient, set retry on connection failure to true
OkHttpClient client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
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