联网时报错:Cleartext HTTP traffic to xxx not permitted
高版本不支持http请求
需要在manifes里加一句 android:usesCleartextTraffic=“true”
emm,
还有,虚拟机里用localhost/127.0.0.1测试出错
应该用主机的地址.
还有get请求服务器的代码
public class GetTask extends AsyncTask {
private final static String path="http://ip地址:8080/api/user/testSend";
@Override
protected Object doInBackground(Object[] objects) {
String name = objects[0].toString();
String pwd = objects[1].toString();
try {
URL url = new URL(path + "?name=" + name + "&pwd=" + pwd);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
return br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
}
返回的数据获取:
GetTask getTask=new GetTask(); message = (String) getTask.execute(email,password).get();