Php 为什么我的HttpPost没有';不行?
我有个问题。我正在尝试为Php 为什么我的HttpPost没有';不行?,php,android,mysql,xampp,slim,Php,Android,Mysql,Xampp,Slim,我有个问题。我正在尝试为Android制作restapi,但是我的HttpPost不起作用。我正在使用XAMPP和Slim框架来解决这个问题 我创建了我的POST方法,我想在表cars中添加一个新的Car 我的Car对象的构造函数是: public Car(int idCar, String name, Date dateBuyCar) { this.idCar = idCar; this.name = name; this.dateBuyCar = dateBuyCar
Android
制作restapi
,但是我的HttpPost
不起作用。我正在使用XAMPP
和Slim框架来解决这个问题
我创建了我的POST
方法,我想在表cars
中添加一个新的Car
我的Car
对象的构造函数是:
public Car(int idCar, String name, Date dateBuyCar)
{
this.idCar = idCar;
this.name = name;
this.dateBuyCar = dateBuyCar;
}
我的POST
方法与Slim
类似:
$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();
}
});
我的HttpPost
方法如下:
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(){
}
}
我认为问题在于我的对象中有一个日期
,但我对此不放心。我认为这可能是问题所在,因为我看到phpmyadmin
中的日期存储方式类似于2010-12-06
,当我使用我的应用程序时,我需要类似于06-12-2010
的日期,所以在应用程序内部我解析它。我再次解析它,以输入新汽车的日期,格式与phpmyadmin
中的格式相同(如异步任务中所示)
我知道所有的信息都直接到达AsyncTask
,因为我用一些日志检查了它,但是当它到达HttpPost
时,什么也没有发生
有什么问题吗
p.S:我知道使用XAMPP
与数据库的连接是正确的,因为我使用了GET
方法,它工作正常
提前谢谢 问题是我在AsyncTask
和POST
方法中使用了不同的ID
例如,当我试图在我的POST
方法中检索我的AsyncTask
中的信息时:
$idCar = $app->request->post("idCar");
$name = $app->request->post("name");
$dateCar = $app->request->post("dateCar");
BasicNameValuePair idCarValuePair = new BasicNameValuePair("id", newCar.getIdCar());
BasicNameValuePair nameValuePair = new BasicNameValuePair("name", newCar.getName());
BasicNameValuePair dateValuePair = new BasicNameValuePair("date", newCar.getDateBuyCar());
我正在使用ID的
:idCar
,name
和dateCar
但是当我试图将信息发送到我的AsyncTask
上的POST
方法时:
$idCar = $app->request->post("idCar");
$name = $app->request->post("name");
$dateCar = $app->request->post("dateCar");
BasicNameValuePair idCarValuePair = new BasicNameValuePair("id", newCar.getIdCar());
BasicNameValuePair nameValuePair = new BasicNameValuePair("name", newCar.getName());
BasicNameValuePair dateValuePair = new BasicNameValuePair("date", newCar.getDateBuyCar());
我使用的是ID的
:ID
,名称
和日期
因此POST
方法无法从AsyncTask
检索信息,因为我使用的ID
与POST
方法有不同的方面
为了解决这个问题,我必须根据我在AsyncTask
中发送的信息更改ID的
。代码如下所示:
BasicNameValuePair idCarValuePair = new BasicNameValuePair("idCar", newCar.getIdCar());
BasicNameValuePair nameValuePair = new BasicNameValuePair("name", newCar.getName());
BasicNameValuePair dateValuePair = new BasicNameValuePair("dateCar", newCar.getDateBuyCar());
你把权限插入清单了吗?@SimoV8是的,我有。你能用stacktrace发布locgat吗?你的标识符在$idCar=$app->request->post(“idCar”)$名称=$app->request->post(“名称”)$dateCar=$app->request->post(“dateCar”)代码>与BasicNameValuePair idCarValuePair=new BasicNameValuePair(“id”,newCar.getIdCar())中的不匹配;BasicNameValuePair nameValuePair=新的BasicNameValuePair(“name”,newCar.getName());BasicNameValuePair dateValuePair=新的BasicNameValuePair(“日期”,newCar.getDateBuyCar())代码>。如果你在这个网站上四处看看,你早就发现了。如果你的客户不阅读echo json_encode($cars,json_UNESCAPED_UNICODE),你怎么能说现在它可以工作呢代码>?什么有效?