状态 200 = HttpPut 有效?
Status 200 = HttpPut works?
我正在尝试修改我的数据库的值 (MySQL
)。对于这个问题,我在 Android
应用程序上使用 HttPut
,Slim framework
作为 PHP 的微框架,XAMPP
用于管理数据库。
问题是它没有修改我的数据库,但是当我使用 response.getStatusLine().getStatusCode()
.
时它给我状态 200
如果我没理解,当您收到状态 200 时,那是因为请求已成功。如您所见:status 200 OK
The request has succeeded.
我错过了什么吗?我理解错了吗?为什么可能是数据库没有改变?
P.S. 如果您需要我提供一些代码,请在评论中告诉我。
编辑: 这是我 PHP script
.
中的 put
方法
$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();
}
});
编辑 2:这是我使用 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() {
}
}
200 状态没有说明您的更新。
- 你选择的数据库连接正确吗?
- 您的查询看起来很正常,但我看不到参数值。您确定它们包含正确的值吗?
- 可以通过bindParam方法指定数据类型
- 你做 'update',所以 lastInsertId 用于 'insert'
但是,您可以这样做:
print_r([$name,$description,$idCar]); // temporaly line for debugging!
$dbh = $connection->prepare("UPDATE cars SET name = ?, description = ? WHERE idCar = ?");
$dbh->execute([$name,$description,$idCar]);
如果使用
终于,我得到了解决方案。我不知道如何在我的 HttpPut
方法上查看我的应用程序正在使用哪些数据,因此通过在 Internet 上搜索,我终于找到了一种查看正在使用哪些数据的方法。内容如下:
执行响应后,您可以输入以下两行:
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
然后您可以通过 Log
:
查看在您的 HttpPut
中发送的数据
Log.d("response",responseText);
通过这种方法,我看到了正在发送的数组,它还给我一个错误,告诉我我 cannot add or update a child row
。这是因为 $idCar
在另一个 table 中有一个引用(级联),而我发送的值在另一个 table 中不存在。
当我发送一个存在于第二个 table 中的值时,它工作正常!
编辑: 要查看您正在使用的值的数组,您必须输入
print_r([$name,$description,$idCar]);
在您的 php script
上,在您的 put 方法中并在设置您的变量值之后。像这样:
$name = $app->request->put("name");
$description = $app->request->put("description");
$idCar = $app->request->put("idCar");
print_r([$name,$description,$idCar]);
我正在尝试修改我的数据库的值 (MySQL
)。对于这个问题,我在 Android
应用程序上使用 HttPut
,Slim framework
作为 PHP 的微框架,XAMPP
用于管理数据库。
问题是它没有修改我的数据库,但是当我使用 response.getStatusLine().getStatusCode()
.
如果我没理解,当您收到状态 200 时,那是因为请求已成功。如您所见:status 200 OK
The request has succeeded.
我错过了什么吗?我理解错了吗?为什么可能是数据库没有改变?
P.S. 如果您需要我提供一些代码,请在评论中告诉我。
编辑: 这是我 PHP script
.
put
方法
$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();
}
});
编辑 2:这是我使用 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() {
}
}
200 状态没有说明您的更新。
- 你选择的数据库连接正确吗?
- 您的查询看起来很正常,但我看不到参数值。您确定它们包含正确的值吗?
- 可以通过bindParam方法指定数据类型
- 你做 'update',所以 lastInsertId 用于 'insert'
但是,您可以这样做:
print_r([$name,$description,$idCar]); // temporaly line for debugging!
$dbh = $connection->prepare("UPDATE cars SET name = ?, description = ? WHERE idCar = ?");
$dbh->execute([$name,$description,$idCar]);
如果使用
终于,我得到了解决方案。我不知道如何在我的 HttpPut
方法上查看我的应用程序正在使用哪些数据,因此通过在 Internet 上搜索,我终于找到了一种查看正在使用哪些数据的方法。内容如下:
执行响应后,您可以输入以下两行:
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
然后您可以通过 Log
:
HttpPut
中发送的数据
Log.d("response",responseText);
通过这种方法,我看到了正在发送的数组,它还给我一个错误,告诉我我 cannot add or update a child row
。这是因为 $idCar
在另一个 table 中有一个引用(级联),而我发送的值在另一个 table 中不存在。
当我发送一个存在于第二个 table 中的值时,它工作正常!
编辑: 要查看您正在使用的值的数组,您必须输入
print_r([$name,$description,$idCar]);
在您的 php script
上,在您的 put 方法中并在设置您的变量值之后。像这样:
$name = $app->request->put("name");
$description = $app->request->put("description");
$idCar = $app->request->put("idCar");
print_r([$name,$description,$idCar]);