如何使用Java发送GET请求
我试图在我的Android应用程序中发送GET请求。我编写了以下代码,但在我使用调试器检查时,它给出了某种SSL错误。 下面是我通过HTTP GET登录的代码如何使用Java发送GET请求,java,android,Java,Android,我试图在我的Android应用程序中发送GET请求。我编写了以下代码,但在我使用调试器检查时,它给出了某种SSL错误。 下面是我通过HTTP GET登录的代码 private class AsyncLogin extends AsyncTask<String, String, String> { ProgressDialog pdLoading = new ProgressDialog(LoginActivity.this); HttpsURLConnection c
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();
}
}
}
为什么要强制转换到HttpsURLConnection,正常的HttpURLConnection也应该这样做,并且可以与HTTP和HTTPS一起工作。
接下来我将替换e.printStackTrace;对于Log.eTAG,创建HTTP连接时出错,例如;,然后你看看会发生什么。
你能把Stacktrace贴在这里吗?然后我们可以看到它是什么错误
顺便说一句:
康涅狄格州塞多因布图;
康涅狄格州塞杜特普特鲁;
对于简单的GET请求不需要。也许您可以告诉我们什么类型的错误。@stdunbar无错误记录只会终止我的应用程序进程。添加一些Log.dTAG,您的消息。。;这是错误粘贴的,让我编辑我不强制转换它日志中没有stacktrace,原因是:java.lang.ClassCastException:com.android.okhttp.internal.http.HttpURLConnectionImpl无法强制转换为javax.net.ssl.httpsurlConnection,因为我怀疑强制转换是问题所在。更改类型并强制转换为HttpURLConnection,然后重试。仍然存在相同的错误
org.apache.harmony.security.fortress.Engine.getInstance