我想从Android应用程序发送名称和姓名到我的服务器。但无法获得连接。我已经使用了大部分可用的代码,但他们都没有工作。以下是我的代码的快照:从Android应用程序到服务器的HTTP请求
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 ... –
检查Internet的权限和尝试ping服务器 – VVB
你得到哪些异常? –