gpt4 book ai didi

Android volley 错误 : "Trust anchor for certification path not found", 仅在真实设备中,而不是模拟器

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:10:21 26 4
gpt4 key购买 nike

我的 Android 应用程序出现问题,在我使用 volley 执行网络请求的 fragment 之一中:

JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
CustomNetworkManager.getInstance(this.getActivity().getApplicationContext()).getRequestUrl(url),
requestData,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// process response
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("FeedFragment", "Volley error: " + error.toString());
}
});

在真实设备上我收到以下错误(运行 API23):

D/FeedFragment: Volley error: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

在运行相同 API 版本的 AVD 中,它工作正常。我检查了其他类似的主题,但找不到答案。

感谢您的帮助。

编辑:如果有人遇到同样的错误,请确保您的证书没有任何问题 (http://developer.android.com/intl/pt-br/training/articles/security-ssl.html#CommonProblems)

最佳答案

尝试将此功能添加到您的应用程序中:

    /**
* Enables https connections
*/
@SuppressLint("TrulyRandom")
public static void handleSSLHandshake() {
try {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
} catch (Exception ignored) {
}
}

然后在您的应用程序 onCreate 中调用它。

更新:

This code is not relevant and shouldn't be used! it is forbidden by Google. for more information look here.

关于Android volley 错误 : "Trust anchor for certification path not found", 仅在真实设备中,而不是模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36043324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com