1

I've got this code on an Android 2.3 Gingerbread device which takes like 30 seconds.

I run it in async task away from UI thread (but it doesn't make a difference where I run it)

long startTime = System.nanoTime();
OkHttpClient client = new OkHttpClient.Builder()
        .followRedirects(false)
        .build();
long clientBuilt = System.nanoTime();
Log.d("API", String.format("Created OkHttpClient, it took %f milliseconds", (clientBuilt - startTime)/1000000d));
D/API: Created OkHttpClient, it took 30814.207720 milliseconds

I have up-to-date version of OkHttp for this Android version: 'com.squareup.okhttp3:okhttp:3.12.0'

Edit:

After applying Martin's suggested code: .connectionSpecs(Collections.singletonList(ConnectionSpec.CLEARTEXT))

and running a call to the server I get an exception:

java.io.IOException: unexpected end of stream on Connection{motoactv.com:443, proxy=DIRECT hostAddress=motoactv.com/69.10.180.44:443 cipherSuite=none protocol=http/1.1}
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:208)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        ...
Caused by: java.io.EOFException: \n not found: limit=0 content=…
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:236)
        at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
        at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
Kamil
  • 1,456
  • 4
  • 32
  • 50
  • I suspect that there is something wrong with your device. I put your code into a scrap project, and it runs in around 0.3 seconds on modern hardware. However, there should not be a 1000x difference to an Android 2.3 device. – CommonsWare Apr 13 '19 at 20:31
  • @Kamil are you behind a proxy? If so check this: https://stackoverflow.com/questions/37866902/okhttp-proxy-settings – madlymad Apr 13 '19 at 21:38
  • @madlymad unofortunately not, no proxy here – Kamil Apr 14 '19 at 07:53

1 Answers1

0

Such old API do not support SNI yet; enforcing plain-text HTTP might help:

OkHttpClient client = new OkHttpClient.Builder()
    .connectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT))
    .followRedirects(false)
    .build();

Stepping into the OkHttpClient.Builder should also tell, where exactly it gets stuck.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • There's no `ConnectionSpec.NONE` in 3.12, only: `CLEARTEXT`, `COMPATIBLE_TLS`, `MODERN_TLS`, `RESTRICTED_TLS` – Kamil Apr 13 '19 at 20:51
  • @Kamil I've meant `CLEARTEXT`... without `SNI` support, most current certificates will even be rejected, so there is no use in trying to use it, even. decompiling with Fernflower would permit stepping into - just alike adding it as a local library module would permit a closer inspection. – Martin Zeitler Apr 13 '19 at 20:53
  • Gonna give it a try, FYI, connectionSpecs() takes a list as an argument – Kamil Apr 13 '19 at 20:56
  • @Kamil I've fixed that. – Martin Zeitler Apr 13 '19 at 20:57
  • I've just edited my question to include the exception I get after applying your code and running a call to the server. The client is in fact created a lot faster, like milliseconds, but then the calls are failing – Kamil Apr 13 '19 at 21:20