gpt4 book ai didi

android - 如何修复由对等 android 关闭的 javax.net.ssl.sslexception 连接

转载 作者:行者123 更新时间:2023-11-29 23:01:39 35 4
gpt4 key购买 nike

我正在尝试使用“Volley”库从“电影数据库”获取 Json 数据,但它显示类似“javax.net.ssl.sslexception 连接被对等方关闭”的错误,所以请帮助解决这个问题。 ..

public class MainActivity extends AppCompatActivity {

private TextView textView;
String uri = "https://api.themoviedb.org/3/movie/popular?api_key=<MY_API_KEY>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.titleid);
loadtext();
}

private void loadtext() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, uri, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try {
JSONArray resultArray = response.getJSONArray("results");
JSONObject obj = resultArray.getJSONObject(1);
String name = obj.getString("title");
textView.setText(name);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText("Error" + error);
error.printStackTrace();
}
});
MySingleton.getInstance(MainActivity.this).addToRequestQueue(jsonObjectRequest);
}
}

最佳答案

  1. 创建一个新类“MySSLSocketFactory”并扩展SSLSocketFactory
  2. 复制并粘贴这段代码:

    SSLSocketFactory sslSocketFactory;

    public MySSLSocketFactory(SSLSocketFactory sslSocketFactory) {
    super();
    this.sslSocketFactory = sslSocketFactory;
    }

    @Override
    public String[] getDefaultCipherSuites() {
    return sslSocketFactory.getDefaultCipherSuites();
    }

    @Override
    public String[] getSupportedCipherSuites() {
    return sslSocketFactory.getSupportedCipherSuites();
    }

    @Override
    public SSLSocket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(s, host, port, autoClose);
    socket.setEnabledProtocols(new String[]{"TLSv1.2"});
    return socket;
    }

    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.setEnabledProtocols(new String[]{"TLSv1.2"});
    return socket;
    }

    @Override
    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException,
    UnknownHostException {
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(host, port, localHost, localPort);
    socket.setEnabledProtocols(new String[]{"TLSv1.2"});
    return socket;
    }

    @Override
    public Socket createSocket(InetAddress host, int port) throws IOException {
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    socket.setEnabledProtocols(new String[]{"TLSv1.2"});
    return socket;
    }

    @Override
    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
    throws IOException {
    SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(address, port, localAddress, localPort);
    socket.setEnabledProtocols(new String[]{"TLSv1.2"});
    return socket;
    }

3. 在您的代码中的“private void loadtext() {”之后粘贴:

   if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {

try {
ProviderInstaller.installIfNeeded(context);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
sslContext.init(null, null, null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
SSLEngine engine = sslContext.createSSLEngine();
}

经过深思熟虑,它非常适合我。

关于android - 如何修复由对等 android 关闭的 javax.net.ssl.sslexception 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56905389/

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