I’m trying to write a basic account creation for an android project using PHP. I post the script the email, password, and nickname and it queries a database for duplicates and inserts the info if none found. If i just run the php script it works fine, but does not when i try to post the stuff from android. Here is the code:
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;
}
The script should return 0 for double fail, 1 for email fail, 2 for nick fail, and 3 for success. From the android project i am always getting 0, becuase there is already a user in the database with an empty name and nick.
also, print_r($_GET, true)); results in :
Array
(
)
When I run android project I get
Array
(
[email] => Random@someplace.org
[password] => somepassword
[nick] => RandoTheMagnificent
)
if i do it from a browser.
Any ideas?
In your Android app, you are sending parameters using POST, not GET.
You should use GET instead of modify your script to use POST.
P.S. You can also check it by adding
print_r($_POST).