使用okhttp绕过自签名证书后,Picasso中的图像缓存无效

时间:2018-06-09 15:36:59

标签: java android picasso okhttp3

库。

  1. 实施'com.squareup.picasso:picasso:2.4.0'
  2. 实施 'com.jakewharton.picasso:picasso2-okhttp3-下载:1.1.0'
  3. 我正在进行POST HTTPS API调用以检索图像。我可以绕过自签名证书并检索图像。但是当设备离线时图像缓存不起作用。当我进行GET API调用时,图像缓存工作正常。任何帮助/建议都将受到赞赏。

    代码 -

    OkHttp3Downloader downloader = new OkHttp3Downloader(Utility.getHttpClientPicasso(AppConstants.TOKEN, number));
                Picasso picasso = new Picasso.Builder(context).downloader(downloader).listener(new Picasso.Listener() {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                        Log.d("Exception in Picasso", exception.getMessage());
                    }
                }).build();
                picasso.setLoggingEnabled(true);
                picasso.setIndicatorsEnabled(true);
                picasso.load(url).stableKey(number).
                        placeholder(R.drawable.actionabr_profile).
                        error(R.drawable.actionabr_profile).
                        into(ivProfileImage, new Callback() {
                            @Override
                            public void onSuccess() {
                            }
    
                            @Override
                            public void onError() {
    
                            }
                        });
    
    public static OkHttpClient getHttpClientPicasso(String token, String number) {
            try {
    
                // Create a trust manager that does not validate certificate chains
                final TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
                            @Override
                            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                            }
    
                            @Override
                            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                            }
    
                            @Override
                            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                return new java.security.cert.X509Certificate[]{};
                            }
                        }
                };
    
                // Install the all-trusting trust manager
                final SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    
                // Create an ssl socket factory with our all-trusting manager
                final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    
                OkHttpClient.Builder builder = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        final MediaType JSON
                                = MediaType.parse("application/json; charset=utf-8");
                        Map<String, String> map = new HashMap<>();
                        map.put(AppConstants.HEADER_TOKEN, token);
                        map.put(AppConstants.HEADER_PS_NUMBER, psNumber);
                        String requestJsonBody = new Gson().toJson(map);
                        RequestBody body = RequestBody.create(JSON, requestJsonBody);
                        final Request original = chain.request();
                        final Request.Builder requestBuilder = original.newBuilder().
                                addHeader(AppConstants.HEADER_TOKEN, "TOKEN")
                                .addHeader(AppConstants.HEADER_NUMBER, "NUMBER")
                                .post(body);
                        return chain.proceed(requestBuilder.build());
                    }
                });
    
                builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
                builder.hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
    
    
                OkHttpClient okHttpClient = builder.
                        cache(new Cache(Application.getContext().getCacheDir(), Integer.MAX_VALUE)).build();
                return okHttpClient;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    

0 个答案:

没有答案