2017-07-27 4 views
0

私はアンドロイドアプリを書いていますが、これは以前はうまくいきましたが、最近、動作していませんでした。下記のリンクからJSONコードの解析マイアプリ: http://opendata.epa.gov.tw/ws/Data/RainTenMin/?%24format=jsonアンドロイドHttpURLConnectionを取得し続ける302

は、私は私のアプリが動作しない理由は、応答コードが302である、だから私は、インターネット上でグーグルとのgetHeaderField(「場所」)を使用することであることがわかりました私のコード で、私はリダイレクトされたURLは、だから私はhttpsでリンクに私のパースのURLを変更し、しかし、この時間は、 HttpURLConnectionのはIOEexceptionが、私はそれを修正することができますどのように、あなたのためにあなたに感謝スロー https://opendata.epa.gov.tw/ws/Data/RainTenMin/?%24format=json

であることがわかりました助けて。

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!! 
+0

、その後、(名前はhttpsConnectionを使用https://developer.android .com/reference/javax/net/ssl/HttpsURLConnection.html) – Ozan

+0

エラーのログcatを追加してください – aliaksei

+0

話題にならないようにしたいのですが、なぜあなたは[retrofit]しようとしませんか( http://square.github.io/retrofit/)? – crgarridos

答えて

0

何を取得していることはjavax.net.ssl.SSLHandshakeExceptionある

以下は私のコード投げる例外です。あなたがここにこの問題を解決する方法についていくつかの有用な情報を見つけることができます

How to solve javax.net.ssl.SSLHandshakeException Error?

抜粋:あなたはhttpsでそれを変更した場合

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); 
     } 
関連する問題