Code on the android device is:
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
Code on the PHP server is:
$name = $_POST['name'];
$email= $_POST['email'];
$password = $_POST['password'];
$user = $db->storeUser($name, $email, $password);
if($user){
echo json_encode($response). "User Stored Successfully";
}
else{
echo json_encode($response). "Error Occured in Registration";
}
where storeUser is:
public function storeUser($name, $email, $password) {
$result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email','$encrypted_password', '$salt', NOW())");
}
The process works, sends a message back to the android device, but the details are not
inserted into the MySQL database?
All I want is to post the details from an android device and insert them into a MySQL database on my server.
Anyone know why this is being caused, what am I doing wrong?
I figured out what was wrong with my code from above.
The url passed to the httppost in the java/android code did not work as a simple address to the api I am using, I tried http://www.domain.com/index.php? with the ‘?’. Adding this made the code work.
I just went back to basics, looking at what makes a post
here is a reference to the POST article in wikipedia http://en.wikipedia.org/wiki/POST_(HTTP), it helped me to figure out what made up a http post