2017-02-03 196 views
0

我想在我的Android應用程序中發送GET請求,編寫了下面的代碼,但是當我使用調試器進行檢查時,它給出了某種SSL錯誤。 由於是用來登錄我的代碼通過HTTP GET如何使用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(); 

     } 
    } 

} 

我的應用結束了這一次函數被調用。

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

也許你可以告訴我們什麼樣的錯誤。 – stdunbar

+0

@stdunbar沒有錯誤記錄只會殺死我的應用程序進程。 –

+0

添加一些'Log.d(TAG,「您的消息..」);'懷疑代碼,以便您可以找到代碼仍然可用的位置。 –

回答

1

你爲什麼投給HttpsURLConnection,正常HttpURLConnection應該做的,也是與HTTP和HTTPS的作品。 接下來我會用Log.e(TAG, "error creating HTTP connection", e);代替e.printStackTrace();,然後你會看到會發生什麼。 你可以在這裏發佈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 –