Error404 Error404 - 4 months ago 14
Android Question

Status 200 = HttpPut works?

I'm trying to modify a value of my database (

MySQL
). For this issue I'm using
HttPut
on my
Android
application and
Slim framework
as micro framework of PHP with
XAMPP
for managing the database.

The problem it's that it doesn't modify my database but it gives to me the status 200 when I use
response.getStatusLine().getStatusCode()
.

And if I didn't understood it, when you receive an status 200 it's because the request had been succesful. As you can see here: status 200 OK


The request has succeeded.


Am I missing something? Did I understand it wrong? Why it could be that the database doesn't change?

P.S. If you need that I provide some code, just let me know in a comment.

EDIT: Here it's my
put
method in my
PHP script
.

$app->put("/cars/",function() use($app)
{
$name = $app->request->put("name");
$description = $app->request->put("description");
$idCar = $app->request->put("idCar");

try{
$connection = getConnection();
$dbh = $connection->prepare("UPDATE cars SET name = ?, description = ? WHERE idCar = ?");
$dbh->bindParam(1,$name);
$dbh->bindParam(2,$description);
$dbh->bindParam(3,$idCar);

$dbh->execute();
$connection = null;

header("HTTP/1.1 200");
header("Content-Type:application/json; charset=utf-8");
echo json_encode(array('status' => true),JSON_UNESCAPED_UNICODE );

}catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
});


EDIT 2: Here it's the code in which I use
HttpPut
.

class EditCar extends AsyncTask<Void, Integer, Void> {

private Car editCar;

EditCar(Car editCar) {
this.editCar = editCar;
}

protected void onPreExecute() {
}

protected Void doInBackground(Void... params) {

try {

String url = "http://IP of my computer/project/cars/";

HttpClient httpClient = new DefaultHttpClient();
HttpPut method = new HttpPut(url);

List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();

nameValuePairList.add(new BasicNameValuePair("name", editCar.getName()));
nameValuePairList.add(new BasicNameValuePair("description", editCar.getDescription()));
nameValuePairList.add(new BasicNameValuePair("idCar", Integer.toString(editCar.getIdCar())));

method.setEntity(new UrlEncodedFormEntity(nameValuePairList));
HttpResponse response = httpClient.execute(method);

} catch (Exception ex) {
Log.e("Error", ex.toString());
}

return null;
}

protected void onProgressUpdate() {
}

protected void onPostExecute() {
}
}

Answer

Finally, I got the solution. I didn't know how to see what data was being used on my application on my HttpPut method, so by searching on the Internet finally I found a method to see what data was being used. It's the following:

After executing your response, you can put this two lines:

HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);

And then you can see what data it's being sent in your HttpPut with a Log:

Log.d("response",responseText);

With that method I saw the array that were sending and also it gave to me an error in which it said to me that I cannot add or update a child row. It is because $idCar have a reference (in cascade) in another table and I was sending a value that doesn't exist on that other table.

When I send a value that exists in the second table it works properly!

EDIT: To see the array of the values that you are using you have to put

print_r([$name,$description,$idCar]);

on your php script inside your put method and after setting your variables values. Like this:

$name = $app->request->put("name");
$description = $app->request->put("description");
$idCar = $app->request->put("idCar");

print_r([$name,$description,$idCar]);