gpt4 book ai didi

java - 如何使用Java发送GET请求

转载 作者:行者123 更新时间:2023-12-01 08:56:10 24 4
gpt4 key购买 nike

我试图在我的 Android 应用程序中发送 GET 请求,编写了以下代码,但当我使用调试器检查时,它给出了某种 SSL 错误。给出的是我用于通过 HTTP 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

最佳答案

为什么要转换为 HttpsURLConnection,普通的 HttpURLConnection 也应该这样做,并且适用于 HTTP 和 HTTPS。接下来,我会将 e.printStackTrace(); 替换为 Log.e(TAG, "error create HTTP connection", e);,然后您就会看到会发生什么。你能在这里发布堆栈跟踪吗?然后我们就可以看到它是什么错误。

顺便说一句:
conn.setDoInput(true);
conn.setDoOutput(true);

对于简单的 GET 请求来说不是必需的。

关于java - 如何使用Java发送GET请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42032189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com