改造 Android 在 MySQL 中添加新行
Retrofit Android add new row in MySQL
我在我的项目中使用 Retrofit 库,在 MySQL 数据库中添加一行,我有一些@POST 方法.
的问题
这是我在文件 test_retro_post.php 中使用 POST 方法的 PHP 代码:
<?php
$response = array();
if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['price'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$price = $_POST['price'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO test_retro(id, name, price) VALUES('$id', '$name', '$price')");
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
使用自定义 JSONparser 发送常规 HttpRequest,一切正常:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
// getting JSON Object
// url_create_product is URL to my PHP file with POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
但是在使用 Retrofit 时出现了问题。不会添加新行。
这是接口 Api_post.java 与 Retrofit`s @POST 方法:
public interface Api_post{
@POST("/test_retro/test_retro_post.php")
Call<RequestBody> createProd(@Body RequestBody body);
我的请求体class (RequestBody.java):
public class RequestBody{
int id;
String name;
int price;
RequestBody(int id, String name, int price){
this.id=id;
this.name = name;
this.price = price;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后在 activity 中调用 Retrofit:
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(Url.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api_post api_post = restAdapter.create(Api_post.java);
Call<RequestBody> call = api_post.createProd(new RequestBody(3, "prod_3", 300);
然后在 AsyncTask 中执行这个调用:
call.execute();
MySQL 数据库中的新行未添加。请告诉我这段代码有什么问题?也许我需要自定义转换器?
您的服务器期望将数据作为 POST 参数发送给它,但您正在尝试发送它 JSON。您可以更改服务器以接受 JSON,或更改改装以使用 @Field
注释发送 POST 参数。此外,很确定您希望响应类型为 ResponseBody
,而不是 RequestBody
@POST("/test_retro/test_retro_post.php")
Call<ResponseBody> createProd(@Field("id") int id, @Field("name") String name, @Field("price") int price);
调用 -
Call<ResponseBody> call = api_post.createProd(3, "prod_3", 300);
我在我的项目中使用 Retrofit 库,在 MySQL 数据库中添加一行,我有一些@POST 方法.
的问题这是我在文件 test_retro_post.php 中使用 POST 方法的 PHP 代码:
<?php
$response = array();
if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['price'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$price = $_POST['price'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO test_retro(id, name, price) VALUES('$id', '$name', '$price')");
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
使用自定义 JSONparser 发送常规 HttpRequest,一切正常:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
// getting JSON Object
// url_create_product is URL to my PHP file with POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
但是在使用 Retrofit 时出现了问题。不会添加新行。 这是接口 Api_post.java 与 Retrofit`s @POST 方法:
public interface Api_post{
@POST("/test_retro/test_retro_post.php")
Call<RequestBody> createProd(@Body RequestBody body);
我的请求体class (RequestBody.java):
public class RequestBody{
int id;
String name;
int price;
RequestBody(int id, String name, int price){
this.id=id;
this.name = name;
this.price = price;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后在 activity 中调用 Retrofit:
Retrofit restAdapter = new Retrofit.Builder()
.baseUrl(Url.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api_post api_post = restAdapter.create(Api_post.java);
Call<RequestBody> call = api_post.createProd(new RequestBody(3, "prod_3", 300);
然后在 AsyncTask 中执行这个调用:
call.execute();
MySQL 数据库中的新行未添加。请告诉我这段代码有什么问题?也许我需要自定义转换器?
您的服务器期望将数据作为 POST 参数发送给它,但您正在尝试发送它 JSON。您可以更改服务器以接受 JSON,或更改改装以使用 @Field
注释发送 POST 参数。此外,很确定您希望响应类型为 ResponseBody
,而不是 RequestBody
@POST("/test_retro/test_retro_post.php")
Call<ResponseBody> createProd(@Field("id") int id, @Field("name") String name, @Field("price") int price);
调用 -
Call<ResponseBody> call = api_post.createProd(3, "prod_3", 300);