Есть два сайта - один использует протокол 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?"; //(не работает)
HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
может быть поэтому? Дляhttp
нужно использоватьHttpURLConnection