5

I am using following dependencies (Retrofit and OkHttp).

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

Below is my code:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(cache_interceptor)
                .cache(cache)
                .connectTimeout(AppConstants.TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(AppConstants.TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(AppConstants.TIME_OUT, TimeUnit.SECONDS)
                .connectionSpecs(Collections.singletonList(ConnectionSpec.CLEARTEXT))
                .retryOnConnectionFailure(false)
                .build();

return new Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BuildConfig.BASE_URL).build();

private final Interceptor cache_interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {

            Request request = chain.request().newBuilder().addHeader("Connection", "close").build();
            return chain.proceed(request);
        }
    };

The URL is https one, pointing to AWS S3 instance.

But I am getting following error for https URL end point.

java.io.IOException: unexpected end of stream on Connection{s3.amazonaws.com:443, proxy=DIRECT hostAddress=s3.amazonaws.com/XX.XXX.XXX.XX:443 cipherSuite=none protocol=http/1.1}

Gone through similar threads like Github discussion 1 and Github discussion 2 but could not find a resolution.

If I replace https with http , there is no issue or error as well !!

Any idea what might be causing the problem?


EDIT 1:

As I was not sure what was causing the problem, I just commented out OK Http as client from Retrofit.

And surprisingly error was not thrown. !

return new Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BuildConfig.BASE_URL).build();

Anyone wanna contribute to the issue, pls comment or write an answer. Much appreciable!

Sreehari
  • 5,621
  • 2
  • 25
  • 59

3 Answers3

3

unexpected end of stream issue is caused mainly when "Content-Length" mentioned in API response doesn't match the real size of API. Better contact your backend developer whether he is overriding content length.

Janusz
  • 187,060
  • 113
  • 301
  • 369
soul
  • 420
  • 1
  • 3
  • 11
2

Its been more than a month since I edited my question, and putting up a note for others to give valid answers. Till date no solution was provided by SO's I am writing down the solution for future reference.

Although the below is not exactly what I want, I could able to get the expected response by avoiding OkHttpClient !

This was with versions I mentioned in question. The same issues may or may not occur in future release versions.

Yes you read it correctly. Avoided OkHttpClient and tried with Retrofit alone.

return new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BuildConfig.BASE_URL).build();

Even though this prevented from getting the HTTP logs, I could able to get the successful response for both HTTP and HTTPS.

Sreehari
  • 5,621
  • 2
  • 25
  • 59
1

I'm going to contribute, since I tried this with HttpURLConnection and OkHttp3, since all the Apache stuff was deprecated.. and was getting 'closed' connection errors with both methods. I had a simple bash CGI script that I was testing against. I followed every tutorial out there, went down the rabbit hole of self-signed certificates, utf-8 parameters in the header, setting connections to close (again, header settings), passing 'Content-length', etc.. etc.. (all worthwhile endeavors, but not what was keeping me up the last 4 nights). I was able to connect (I could see on the server side what was happening), but whenever I tried popping stuff off the stack to read the results, I'd get the connection error. My bash CGI was super simple. The 'Content-type: text/html;' and blank line and some output like 'hello world' (which all worked from a browser). So. my advice since there was no documentation or help anywhere on this. Point your app to google.com. If no errors on your app, then I'd bet dollars to donuts, it's a server side issue. In my case (and for the life of me this can't make any sense, but it does), what none of the solutions showed was that these app methods expect all the bells and whistles from a web page, which means, "head" and "body" and throw in an "html" for good measure.

Just because your browser will display it correctly, is no guarantee that the api libraries are going to handle it well, so I'll repeat.. point your app to a web source that you can reasonably expect to have things formatted, and then take it from there.

  • I had gone through several threads regarding the same. Found out in my trial and errors that this issue is with OkHttpClient client library. Without this library , retrofit was able to give the expected output – Sreehari Aug 27 '19 at 07:37