有两个站点 - 一个使用 https 协议,另一个使用 http。它们加载了相同的 api.php 文件。(实际上这两个站点都是彼此的副本)。网站如下所示:
请求本身是:
答案应该来了:server_connect。
如果您通过浏览器行发出请求,那么在这两种情况下一切都很好。但是,如果您通过应用程序发出请求,那么在第一种情况下(https ...)一切正常,但在第二种情况下,应用程序在发出相同请求时会崩溃。
资料来源:
GET 请求的类:
class ProgressTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... path) {
String content;
try {
content = getContent(path[0]);
} catch (IOException ex) {
content = ex.getMessage();
}
return content;
}
@Override
protected void onPostExecute(String content) { //метод для получения ответа
if (content.equals("server_connect")){
Intent intent = new Intent(MainActivity.this, MainCl.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),R.string.server_error, Toast.LENGTH_LONG).show();
}
pb.setVisibility(View.INVISIBLE);
btn.setText(R.string.next);
btn.setEnabled(true);
}
private String getContent(String path) throws IOException {
BufferedReader reader = null;
try {
URL url = new URL(path);
HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setReadTimeout(PublicVar.time);
c.connect();
reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder buf = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
buf.append(line);
}
return (buf.toString());
} finally {
if (reader != null) {
reader.close();
}
}
}
}
GET 请求本身:
new ProgressTask().execute((PublicVar.URL + "server_connect=" + "VersionApp: "
+ BuildConfig.VERSION_NAME + ";_SDK:_"+ Integer.toString(Build.VERSION.SDK_INT) + ";_ModelPhone:_" + android.os.Build.MODEL.toString()).replaceAll(" ", "_"));
类中的变量PublicVar:
static final String URL = "https://www.site.000webhost.com/api.php?"; //(работает)
或者
static final String URL = "http://www.site.online/api.php?"; //(не работает)