英文:
android http URL connection working with https, not working with http
问题
以下是翻译好的代码部分:
我尝试使用以下代码执行http GET请求。对于https URL字符串,它能够正常工作,但对于http URL则无法正常工作。
我不明白为什么会这样。会抛出一个IOException异常。
你能帮忙吗?我尝试检索相同的URL,例如Google,使用https和不使用https,后者可以正常工作。问题是什么?我已经在清单文件中添加了用户访问互联网的权限。
package com.example.httptest;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
class GetData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection urlConnection = null;
String result = "";
try {
//URL url = new URL("https://www.fineco.it");
URL url = new URL("https://www.google.com");
Log.e("MAU","0");
urlConnection = (HttpURLConnection) url.openConnection();
Log.e("MAU","1");
int code = urlConnection.getResponseCode();
Log.e("MAU","2");
if (code == 200) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (in != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
}
in.close();
}
Log.e("MAU",result);
return result;
} catch (MalformedURLException e) {
Log.e("MAU","Malformed URL");
e.printStackTrace();
} catch (IOException e) {
Log.e("MAU","IOException");
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return result;
}
}
请注意,这只是你提供的代码的翻译部分,没有其他额外的内容。如果你需要进一步的帮助或解释,请随时提问。
英文:
I have tried to perform an http GET by using the following code. It works perfectly with https URL string, instead with http url doesn't work.
I Do not understand why. An IOException is given.
Can you help? I have tried to retrieve the same URL, for instance google, with and without, https, the latter is working. What's the issue? I have added in the manifest the users permission to access internet.
package com.example.httptest;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
class GetData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection urlConnection = null;
String result = "";
try {
//URL url = new URL("https://www.fineco.it");
URL url = new URL("https://www.google.com");
Log.e("MAU","0");
urlConnection = (HttpURLConnection) url.openConnection();
Log.e("MAU","1");
int code = urlConnection.getResponseCode();
Log.e("MAU","2");
if (code == 200) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (in != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
}
in.close();
}
Log.e("MAU",result);
return result;
} catch (MalformedURLException e) {
Log.e("MAU","Malformed URL");
e.printStackTrace();
} catch (IOException e) {
Log.e("MAU","IOException");
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return result;
}
}
答案1
得分: 0
将以下行添加到您的androidManifest.xml文件的application标签中,然后尝试进行调试:
android:usesCleartextTraffic="true"
英文:
Add this lines to your androidManifest.xml in application tag & try debugging it
android:usesCleartextTraffic="true"
答案2
得分: 0
我找到了这篇帖子,解决了这个问题:
https://medium.com/mindorks/my-network-requests-are-not-working-in-android-pie-7c7a31e33330
英文:
I managed to find this post which solved the issue
https://medium.com/mindorks/my-network-requests-are-not-working-in-android-pie-7c7a31e33330
专注分享java语言的经验与见解,让所有开发者获益!
评论