我有一个连接到LAMP服务器并使用AsyncTask提取一些数据的项目。此应用程序可以在从API 16到API 26的所有Android API上正确运行。但是,当在Android 9(API 28)上进行检查时,我在变量“ res”下面显示的代码中没有提供任何数据。 为什么?
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class loginserver extends AsyncTask {
private String Link = "";
private String User = "";
private String Pass = "";
public loginserver(String link, String user, String pass) {
Link = link;
User = user;
Pass = pass;
}
@Override
protected Object doInBackground(Object[] objects) {
try {
String data = URLEncoder.encode("username", "UTF8") +
"=" + URLEncoder.encode(User, "UTF8");
data += "&" + URLEncoder.encode("password", "UTF8") +
"=" + URLEncoder.encode(Pass, "UTF8");
URL url = new URL(Link);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(urlConnection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
SecondActivity.res = stringBuilder.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:1)
鉴于API 28出了问题,我怀疑这与this有关。该链接指向一个Android开发者博客文章,其中详细介绍了Android P上引入的与网络安全相关的限制,除非您采取措施将某些域列入白名单,否则基本上禁止明文通信。