2017-02-03 2 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을 통해 로그인하는 데 사용 내 코드입니다. 당신이 HttpsURLConnection 캐스팅 이유는 무엇

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

아마도 어떤 종류의 오류가 발생했는지 알 수 있습니다. – stdunbar

+0

@stdunbar 기록 된 오류없이 내 앱 프로세스가 종료됩니다. –

+0

의심스러운 코드에'Log.d (TAG, your message .. ");를 추가하면 코드가 여전히 작동하는 곳을 찾을 수 있습니다. –

답변

1

는 정상 HttpURLConnection도 수행하고 HTTP 및 HTTPS와 함께 작동합니다. 다음으로 e.printStackTrace();Log.e(TAG, "error creating HTTP connection", e);으로 대체하면 어떤 일이 일어나는 지 알 수 있습니다. Stacktrace를 여기에 게시 할 수 있습니까? 그러면 어떤 오류인지 알 수 있습니다.

은 BTW : conn.setDoInput(true); conn.setDoOutput(true);

는 간단한 GET 요청에 대한 필요가 없습니다.

+0

실수로 붙여 넣었습니다. 캐스팅하지 않았습니다. –

+0

로그에 stacktrace가 없습니다. –

+0

에 의해 발생했습니다. java.lang.ClassCastException : com.android.okhttp.internal.http.HttpURLConnectionImpl을 javax.net으로 형변환 할 수 없습니다. .ssl.HttpsURLConnection –

관련 문제