2017-02-03 9 views
0

私はAndroidアプリケーションでGETリクエストを送信しようとしていますが、次のコードを書いていますが、デバッガを使用して検査したときにSSLエラーが発生します。考える はJavaを使用してGETリクエストを送信するには

private class AsyncLogin extends AsyncTask<String, String, String> 
{ 
    ProgressDialog pdLoading = new ProgressDialog(LoginActivity.this); 
    HttpsURLConnection conn; 
    URL url = null; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pdLoading.setMessage("\tLoading..."); 
     pdLoading.setCancelable(false); 
     pdLoading.show(); 

    } 
    @Override 
    protected String doInBackground(String... params) { 
     try { 
      url = new URL(params[0]); 

     } catch (MalformedURLException e) { 

      e.printStackTrace(); 
      return "exception"; 
     } 
     try { 
      conn = (HttpsURLConnection) url.openConnection(); 
      conn.setReadTimeout(READ_TIMEOUT); 
      conn.setConnectTimeout(CONNECTION_TIMEOUT); 
      conn.setRequestMethod("GET"); 
      conn.setRequestProperty("Content-Type", "application/json"); 
      conn.setRequestProperty("Accept", "application/json"); 
      conn.setRequestProperty("Origin","https://myserver.com"); 
      conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.connect(); 

     } catch (IOException e1) { 
      e1.printStackTrace(); 
      return "exception"; 
     } 

     try { 

      int response_code = conn.getResponseCode(); 
      if (response_code == HttpsURLConnection.HTTP_OK) { 
       InputStream input = conn.getInputStream(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
       StringBuilder result = new StringBuilder(); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        result.append(line); 
       } 
       return(result.toString()); 

      }else{ 

       return("unsuccessful"); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
      return "exception"; 
     } finally { 
      conn.disconnect(); 
     } 


    } 

    @Override 
    protected void onPostExecute(String result) { 
     pdLoading.dismiss(); 

     if(result.equalsIgnoreCase("true")) 
     { 
      Intent intent = new Intent(LoginActivity.this,SuccessActivity.class); 
      startActivity(intent); 
      LoginActivity.this.finish(); 

     }else if (result.equalsIgnoreCase("false")){ 
      Toast.makeText(getApplicationContext(), "Invalid email or password", Toast.LENGTH_LONG).show(); 
     } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) { 

      Toast.makeText(LoginActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show(); 

     } 
    } 

} 

私のアプリは一度この関数が呼び出されて終わるHTTP GET経由でログインするために使用私のコードです。

org.apache.harmony.security.fortress.Engine.getInstance 
+3

おそらく、どのようなエラーが発生したかを教えてください。 – stdunbar

+0

@stdunbarログに記録されるエラーは、私のアプリプロセスを殺すだけではありません。 –

+0

'Log.d(TAG、あなたのメッセージ.."); 'コードを疑わしいコードに追加して、コードがまだ動作する場所を見つけられます。 –

答えて

1

あなたはHttpsURLConnectionにキャストなぜ、通常のHttpURLConnectionも行い、HTTPおよびHTTPSで動作する必要があります。 次はe.printStackTrace();Log.e(TAG, "error creating HTTP connection", e);に置き換えて、何が起こるかを確認します。 ここにStacktraceを投稿できますか?次に、それがどのようなエラーであるかを見ることができます。ところで

conn.setDoInput(true); conn.setDoOutput(true);

は、単純なGETリクエストには必要ありません。

+0

これは間違って貼り付けられました編集しましょうキャストしていません –

+0

ログにスタックトレースがありません –

+0

原因:java.lang.ClassCastException:com.android.okhttp.internal.http.HttpURLConnectionImplをjavax.netにキャストできません.ssl.HttpsURLConnection –

関連する問題