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

时间:2019-07-05 14:55:42

标签: android

我正在尝试使用“ Volley”库从“ Movie DB”中获取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);
    }
}

2 个答案:

答案 0 :(得分:1)

  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();
    }

经过这么多思考,它对我来说很完美。

答案 1 :(得分:0)

当您遇到错误时,您的代码很好,我正在添加代码并定义可能的检查。

代码

String url = "https://api.themoviedb.org/3/movie/popular?api_key=434fcadef5103207fecca9176385a533";
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("moive", "response: " + response);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(jsonObjectRequest);

权限检查

 <uses-permission android:name="android.permission.INTERNET" />
  

如果api级别为28,请添加此

内部应用标签...

android:usesCleartextTraffic="true"