简体   繁体   中英

OkHttp Interceptor and CookieJar dont work on most recent Huawei like P40

My app is using OkHttp 3 and Retrofit2 for networking. It communicates with server, that returns session after successful login. I need to save this session and attach it to request header for all requests that will be executed after login.

I have used Cookie Jar for this and it worked just fine on emulator and my Samsung phone, but my client reported problem on Huawei P40 Lite. After some log checking on server, I figured that session wasnt sent to server and it caused verification problem.

After that I used Huawei live debuging to test my app on remote Huawei P40 device and reported problem persisted. After some playing around with my app and testing I concluded that OkHttp 3 Cookie Jar and Interceptor that i use to handle saving and attaching session were not working on Huawei at all. Could this be related to Google services ban for newest Huawei devices as app is working fine on older ones like P30?

Any tips or solutions are more than welcome.

I have overcame this by manually extracting session from response and attaching it to my calls with @Header, but this solution is very unclean.

Here is my code for OkHttpClient. Keep in mind it worked and it still is working just fine in production for over 2 years on non newest Huawei phones.

return new OkHttpClient.Builder()
    .sslSocketFactory(sslContext.getSocketFactory(), loadX509TrustManager(tmf))
    .hostnameVerifier(hostnameVerifier)
    .readTimeout(60, TimeUnit.SECONDS)
    .writeTimeout(60, TimeUnit.SECONDS)
    .cookieJar(new JavaNetCookieJar(cookieManager))
    .cache(cache)
    .addNetworkInterceptor(logInterceptor)
    .build();

UPDATE

I was wrong about Cookie Jar and Interceptors not working. I had try catch block with this line: ProviderInstaller.installIfNeeded(context); that would fail because it requires google services, and in catch blok I would retrun a OkHttpClient that has no Cookie Jar attached.

I checked if there is a similar Huawei replacement for: ProviderInstaller.installIfNeeded(context); but I couldn't find one. This line is used to update any missing security provider patches if they were missing. Google documentation about mentioned line.

Try another Cookie jar implementation? Perhaps this persistent one ? There might be a bad CookieManager on the P40.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM