Error404 Error404 - 4 months ago 6
PHP Question

Why my HttpPost doesn't work?

I have a problem. I'm trying to make a

REST API
for
Android
but my
HttpPost
doesn't work. I'm using
XAMPP
and
Slim framework
for this issue.

I have my
POST
method created in which I want to add one new
Car
to the table
cars
.

The constructor of my
Car
object is:

public Car(int idCar, String name, Date dateBuyCar)
{
this.idCar = idCar;
this.name = name;
this.dateBuyCar = dateBuyCar;
}


My
POST
method with
Slim
it's like:

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

try{
$connection = getConnection();
$dbh = $connection->prepare("INSERT INTO cars VALUES(?,?,?)");
$dbh->bindParam(1,$idCar);
$dbh->bindParam(2,$name);
$dbh->bindParam(3,$dateCar);

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

header("HTTP/1.1 200");
header("Content-Type:application/json; charset=utf-8");
echo json_encode($cars,JSON_UNESCAPED_UNICODE );

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


And I make my
HttpPost
method like this:

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

private Car newCar;

addCarDatabase(Car newCar)
{
this.newCar = newCar;
}

protected void onPreExecute(){
}

protected Void doInBackground(Void... params) {

String date = null;

date = new SimpleDateFormat("yyyy-MM-dd").format(newCar.getDate());

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

HttpClient httpClient = new DefaultHttpClient();

HttpPost method = new HttpPost(url);

BasicNameValuePair idCarValuePair = new BasicNameValuePair("id", newCar.getIdCar());
BasicNameValuePair nameValuePair = new BasicNameValuePair("name", newCar.getName());
BasicNameValuePair dateValuePair = new BasicNameValuePair("date", newCar.getDateBuyCar());


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

nameValuePairList.add(idCarValuePair);
nameValuePairList.add(nameValuePair);
nameValuePairList.add(dateValuePair);

try{

UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
method.setEntity(urlEncodedFormEntity);

} catch (UnsupportedEncodingException uee) {
System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
uee.printStackTrace();
}

try {
HttpResponse response = httpClient.execute(method);
Log.d("Response", response.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

protected void onProgressUpdate(){
}

protected void onPostExecute(){
}
}


And in my
MySQL
database (I use
phpmyadmin
to administrate it) I have three columns, each of them for one of the attributes of the new
Car
that I want to add:

--> id(int)
--> name(String)
--> dt(date)


I think that the problem it's because I have a
Date
in my object but I'm not secure of that. I think it could be the problem because I saw that the dates in
phpmyadmin
are stored like
2010-12-06
and when I use my application I need the date like
06-12-2010
so inside the application I parse it. I parse it again to enter the date of the new
Car
with the format that it has in
phpmyadmin
(Like you can see in the
AsyncTask
).

I know that all the information comes right to the
AsyncTask
because I checked it with some
Logs
but when it arrives to the
HttpPost
nothing happens.

What could be the problem?

P.S: I know that the connection to the database with
XAMPP
it's correct because I make a
GET
method and it works properly.

Thanks in advance!

Answer

The problem was that I used different ID's in the AsyncTask and in the POST method.

For example, when I tried to retrieve on my POST method the information from my AsyncTask:

$idCar = $app->request->post("idCar");
$name = $app->request->post("name");
$dateCar = $app->request->post("dateCar");

I was using the ID's: idCar, name and dateCar.

But when I was trying to send the information to the POST method on my AsyncTask:

BasicNameValuePair idCarValuePair = new BasicNameValuePair("id", newCar.getIdCar());
BasicNameValuePair nameValuePair = new BasicNameValuePair("name", newCar.getName());
BasicNameValuePair dateValuePair = new BasicNameValuePair("date", newCar.getDateBuyCar());

I was using the ID's: id, name and date.

So the POST method couldn't retrieve the information from the AsyncTask because the ID's that I was using there were different respect the POST method.

To solve this problem I had to change the ID's from the information that I was sending on my AsyncTask. The code will look like:

BasicNameValuePair idCarValuePair = new BasicNameValuePair("idCar", newCar.getIdCar());
BasicNameValuePair nameValuePair = new BasicNameValuePair("name", newCar.getName());
BasicNameValuePair dateValuePair = new BasicNameValuePair("dateCar", newCar.getDateBuyCar());