Я пытаюсь отправить имя и фамилию на свой сервер из приложения Android. Но не удалось установить соединение. Я использовал большинство доступных кодов, но никто из них не работает. Ниже приведен снимок моего кода:Http-запрос от приложения android к серверу
private void nextActivity(Profile profile) {
HttpURLConnection con = null;
try {
con = (HttpURLConnection)(new URL("**********/index.php")).openConnection();//not written the url for privacy concern
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
con.getOutputStream().write(("name=" + profile.getFirstName()).getBytes());
con.getOutputStream().write(("surname=" + profile.getLastName()).getBytes());
}
catch (ProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
if (profile != null) {
Intent main = new Intent(LoginActivity.this, MainActivity.class);
main.putExtra("name", profile.getFirstName());
main.putExtra("surname", profile.getLastName());
main.putExtra("imageUrl", profile.getProfilePictureUri(200, 200).toString());
startActivity(main);
}
}
}
Я надеюсь, что вы используете AsyncTask здесь ... –
Проверка на интернет-разрешения и пробуйте пинговать сервер – VVB
который исключение вы получаете? –