我正在尝试使用 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 表示昵称失败,3 表示成功。从android项目我总是得到0,因为数据库中已经有一个用户名和昵称为空。
此外,print_r($_GET, true));
结果:
Array
(
)
当我运行 android 项目时,我得到
Array
(
[email] => Random@someplace.org
[password] => somepassword
[nick] => RandoTheMagnificent
)
如果我从浏览器中执行此操作。
有任何想法吗?