我正试图使我的应用程序与我的php服务器通信,但我困惑这里这个应用程序在我的android手机上工作得很好,但当我发送这个相同的应用程序给我的朋友进行审查时,他们无法注册,因为它抛出IOException。
我知道有许多相关的问题,但没有一个能解决我自己的问题。
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(15000);
con.setConnectTimeout(15000);
try {
if(con.getResponseCode() == 200) {
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
//con.getInputStream().
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "n");
}
return sb.toString().trim();
}
return "400";
} catch (IOException e) {
//401 is returned once they hit the signup button
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
return "401";
}
} catch (Exception e) {
return "402";
}
}
当我的朋友试图注册时,他们得到的是"401"作为我设置的错误码
我已经解决了问题。我发现我朋友的手机运行的是更高版本的android操作系统,而我的8.1.0运行得很好。由于我通过http而不是https进行服务器请求,android 9 pie将不允许出于安全原因。所以我只是通过https而不是http来发出请求。