我想在我的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
也許你可以告訴我們什麼樣的錯誤。 – stdunbar
@stdunbar沒有錯誤記錄只會殺死我的應用程序進程。 –
添加一些'Log.d(TAG,「您的消息..」);'懷疑代碼,以便您可以找到代碼仍然可用的位置。 –