通过POST发送登录细节来自android123
问题描述:
我想从我的android应用程序发送用户的电子邮件地址和密码到数据库通过POST登录。通过POST发送登录细节来自android123
在服务器端,我得到我的数据是这样的:
$email = $_POST['email'];
$password = clean($_POST['password'];
而且在Android方面我把它像这样:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("some real URL");
httppost.setHeader("Content-type", "application/json");
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httppost);
......
即使当我在合法的登录信息输入,它失败了,并说没有电子邮件地址或密码。我是否正确地发送信息?
我也试过在下面发送数据,但没有工作。有什么建议么?
JSONObject obj = new JSONObject();
obj.put("email", email);
obj.put("password", password);
httppost.setEntity(new StringEntity(obj.toString()));
答
HttpPost.setEntity设置请求的主体没有任何名称/值配对,只是原始post数据。 $ _POST不会查找原始数据,只是名称值对,它会将其转换为散列表/数组。您可以格式化请求,使其包含名称值对。
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("json", json.toString()));
httppost.setEntity(new UrlEncodedFormEntity(params));
而且在JSON对象中的参数为:
JSONObject json = new JSONObject();
json.put("email", email);
json.put("password", password);
在服务器端,你可以得到的数据为:
$jsonString = file_get_contents('php://input');
$jsonObj = json_decode($jsonString, true);
if(!empty($jsonObj)) {
try {
$email = $jsonObj['email'];
$password = $jsonObj['password'];
}
}
[Android的HTTP POST的
可能重复不发送数据](http://*.com/questions/10248297/android-http-post-not-sending-data) – cxzp
在其他线程找到答案,问题的内容是相同的答案是头是错的 – cxzp