【问题标题】:android HttpURLConnection keep getting 302android HttpURLConnection 不断收到 302
【发布时间】:2017-07-27 21:56:57
【问题描述】:

我写了一个安卓应用程序,之前它运行良好,但是,最近,我发现它不能运行。我的应用程序从以下链接解析 json 代码: http://opendata.epa.gov.tw/ws/Data/RainTenMin/?%24format=json

我发现我的应用程序不工作的原因是响应码是302,所以我在互联网上google,并在我的代码中使用getHeaderField(“Location”) 我发现重定向的网址是 https://opendata.epa.gov.tw/ws/Data/RainTenMin/?%24format=json

所以我将解析 url 更改为带有 h​​ttps 的链接,但是,这一次, HttpURLConnection 抛出 IOEexception,我该如何解决呢,谢谢你的帮助。

下面是我的代码抛出异常:

private static String httpRequest(URL rurl) throws IOException{
        String response = "";

        if (rurl == null) {
            return response;
        }
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        try {
            connection = (HttpURLConnection) rurl.openConnection();
            connection.setConnectTimeout(20000);
            connection.setRequestMethod("GET");
            connection.setReadTimeout(25000);
            connection.connect();

            //200 means correctly connected
            int responseCode = connection.getResponseCode();

            if (responseCode ==200) {
                inputStream = connection.getInputStream();
                response = readStream(inputStream);
            }
            else {

                MainActivity.connect=false;
                Log.i(logtag, "Error!!  Response code is " + responseCode);
            }
        }
        catch (IOException e) {
            MainActivity.connect=false;
            Log.e(logtag, "bad internet!!");}
        finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return response;
    } 

下面是错误的logcat:

E/query: bad internet!!

【问题讨论】:

标签: android http


【解决方案1】:

你得到的是 javax.net.ssl.SSLHandshakeException。

您可以在此处找到有关如何解决此问题的一些有用信息: How to solve javax.net.ssl.SSLHandshakeException Error?

摘录:

TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {

                    public java.security.cert.X509Certificate[] getAcceptedIssuers()
                    {
                        return null;
                    }
                    public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
                    {
                        //No need to implement.
                    }
                    public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
                    {
                        //No need to implement.
                    }
                }
        };

        // Install the all-trusting trust manager
        try 
        {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } 
        catch (Exception e) 
        {
            System.out.println(e);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2017-02-12
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多