我正在尝试使用 PHP 为一个 android 项目编写一个基本的帐户创建。我发布脚本的电子邮件、密码和昵称,它会查询数据库中的重复项,如果没有找到,则插入信息。如果我只是运行 php 脚本,它可以正常工作,但当我尝试从 android 发布内容时则无法正常工作。这是代码:
public String createUser(String email, String pass, String nick) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("password", pass));
nameValuePairs.add(new BasicNameValuePair("nick", nick));
InputStream is = null;
String reply = null;
try {
HttpClient httpclient = new DefaultHttpClient();
// must store something locally to define this url
// if the url ever changes, gotta make sure droids know without
// requiring an update
HttpPost httppost = new HttpPost(STR_PHP_USER);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
reply = reader.readLine();
Log.e("createUser", "php response is "+reply);
is.close();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString() + " getCreateUserResult_HTTPPost");
}
return reply;
}
脚本应返回 0 表示双重失败,1 表示电子邮件失败,2 返回 2 表示昵称失败,3 表示成功。从 android 项目中,我总是得到 0,因为数据库中已经有一个用户名和昵称为空的用户。
此外,print_r($_GET, true));
结果是:
Array
(
)
当我运行安卓项目时,我得到
Array
(
[email] => Random@someplace.org
[password] => somepassword
[nick] => RandoTheMagnificent
)
如果我从浏览器上做。
有什么想法吗?
在您的 Android 应用中,您使用 POST 而不是 GET 发送参数。
您应该使用 GET 而不是修改脚本以使用 POST。
附言您也可以通过添加 print_r($_POST)
.