OPPO 手機Android6.0系統 Glide 4.0+ 版本 加載HTTPS的圖片顯示空白java
圖片是保存在阿里雲服務上,因此圖片提供的地址都是https://開頭的。整個測試過程一切正常,也準備發版本。忽然在OPPO手機上,圖片不顯示,空白一片。趕忙鏈接上手機進行DEBUG,能夠看到一堆錯誤。有錯誤信息就好處理了。錯誤提示以下: android
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:328)
at com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:103)
at com.android.okhttp.Connection.connect(Connection.java:143)
...
at aoo.run(SourceFile:446)
Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:324)
at com.android.org.conscrypt.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:225)
...
com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
at alm.a(SourceFile:104)
...
at java.lang.Thread.run(Thread.java:818)
at aoo.run(SourceFile:446)
Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:324)
at
...
com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
...
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
at aoo.run(SourceFile:446)
複製代碼
而後進行google錯誤信息「Trust anchor for certification path not found.」,可以搜到的答案就是讓你設置自定義證書。可我並不想設置證書。 再看時間都是2016年或2017年的,使用glide還在2.0+時代,將哪些解決方法用到工程裏,都是報錯的。git
api "com.github.bumptech.glide:okhttp3-integration:4.7.1"
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
複製代碼
我使用的glide 4.7.1版本,全部添加這對應的版本也是4.7.1. 其中添加com.github.bumptech.glide:compiler
是由於,不添加就會報錯誤github
Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
複製代碼
記得同步項目下。api
類的內容:緩存
import android.content.Context;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import java.io.InputStream;
import okhttp3.OkHttpClient;
@GlideModule
public class OkHttpAppGlideModule extends AppGlideModule {
public void registerComponents(Context context, Registry registry) {
OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
複製代碼
類的內容:bash
import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
public class UnsafeOkHttpClient {
public static OkHttpClient getUnsafeOkHttpClient() {
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();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
builder.connectTimeout(20, TimeUnit.SECONDS);
builder.readTimeout(20,TimeUnit.SECONDS);
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
複製代碼
在AndroidManifest.xml中添加申明:session
<meta-data
android:name="com.****.OkHttpAppGlideModule"
android:value="AppGlideModule" />
複製代碼
其中的路徑須要修改成本身項目的路徑。app
再次運行時,成功顯示圖片。dom
小插曲
當把圖片的https://路徑修改成http://進行訪問成功,再次進行https://路徑訪問,盡然成功顯示了。一開始覺得是問題解決了,可是當手機重啓後,https://路徑仍是沒法顯示圖片。
可在添加了一次重複的混淆-keep public class * implements com.bumptech.glide.module.GlideModule
. 盡然能夠了。 而後把這個混淆刪除後,也是成功的。 那麼緣由只能是:glide的緩存問題。當屢次訪問同一個https://失敗後,glide將此https://路徑設置爲無效路徑,再次進行訪問時直接就顯示失敗了。而後修改了下混淆,glide又進行訪問了。(做者的猜想,沒有進行源碼閱讀,有不對的地方請指正。)
參考文章: blog.csdn.net/u010232308/…
// END 2018-6-8 小小的開心吧