我正在重构我的应用以使用Retrofit 2
,我能够使我的所有GET requests
都很好,但我无法理解如何制作POST requests
。
我有这个例外:
java.lang.IllegalStateException:预期为BEGIN_OBJECT但是为STRING 在第1行第1列路径$ e这里
我知道那个异常意味着什么,非常简单。我的问题是我无法更改我的PHP文件以期望字符串,或者更改java类以发送对象。
我对PHP的理解很少,所以我更喜欢改变我的java类。
我想到的不是发送我的java pojo
类,而是发送JSONObject
并将其发送给PHP。
但是,我对PHP文件或我想到的方法不是很有信心。我想告诉我,如果它是一个可能的解决方案,或者如果PHP代码中有什么问题,那就是为什么目前没有工作。
以下是课程:
<?php
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$dbh = $db->connect(); // here you get the connection
$name = $_POST['name'];
$breed = $_POST['breed'];
$type = $_POST['type'];
$description = $_POST['description'];
$pictures = $_POST['pictures'];
$location = $_POST['location'];
$locality = $_POST['locality'];
$userid = $_POST['userid'];
$query = "INSERT INTO lost_pets (name, breed, type, description, pictures, location,locality, userid) VALUES('$name', '$breed', '$type', '$description', '$pictures', '$location', '$locality','$userid')";
$stmt = $dbh->prepare($query);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':breed', $breed, PDO::PARAM_STR);
$stmt->bindParam(':type', $type, PDO::PARAM_STR);
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
$stmt->bindParam(':pictures', $pictures, PDO::PARAM_STR);
$stmt->bindParam(':location', $location, PDO::PARAM_STR);
$stmt->bindParam(':locality', $locality, PDO::PARAM_STR);
$stmt->bindParam(':userid', $userid, PDO::PARAM_STR);
$dbh = $stmt->execute(array(":name"=>$name,":breed"=>$breed,":type"=>$type,":description"=>$description,
":pictures"=>$pictures,":location"=>$location, ":locality"=>$locality, ":userid"=>$userid));
?>
Java界面:
@POST("post_lost_pet.php")
Call<Lost> postLost(@Body Lost lost);
postLost方法:
public void postLost(Lost pet, final IClient client){
Call<Lost> call = retrofitInit().postLost(pet);
call.enqueue(new Callback<Lost>() {
@Override
public void onResponse(Call<Lost> call, Response<Lost> response) {
System.out.println(response.raw());
client.lostCallback();
}
@Override
public void onFailure(Call<Lost> call, Throwable t) {
client.lostFailure(t.toString());
}
});
POJO:
public class Lost extends Pet {
public Lost(String name, String breed, String type, String description, String pictures, String location, String locality, String userid) {
super(name, breed, type, pictures, description, location, locality, userid);
}
public void setLocality(String locality){
super.setLocality(locality);
}
public void setName(String name) {
super.setName(name);
}
}
答案 0 :(得分:0)
问题在于您期望来自服务器的响应,但您没有从php文件中给出任何响应。
在你的PHP文件中添加到最后,
$arr = array('response' => 'success');
echo json_encode($arr);
这将在执行查询后给出响应。 然后在界面中更改预期的响应,
@POST("post_lost_pet.php")
Call<MyResponse> postLost(@Body Lost lost);
您的响应类看起来像这样,
import android.os.Parcel;
import android.os.Parcelable;
public class MyResponse implements Parcelable {
private String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
protected MyResponse(Parcel in) {
response = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(response);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<MyResponse> CREATOR = new Creator<MyResponse>() {
@Override
public MyResponse createFromParcel(Parcel in) {
return new MyResponse(in);
}
@Override
public MyResponse[] newArray(int size) {
return new MyResponse[size];
}
};
}
答案 1 :(得分:0)
我修改了它,在改装调用方法中替换java接口中的参数:
@POST("post_lost_pet.php")
@FormUrlEncoded
Call<Lost> postLost(@Field("name") String name, @Field("breed") String breed, @Field("type") String type,
@Field("pictures") String pictures, @Field("description") String description, @Field("location") String location,
@Field("locality") String locality, @Field("userid") String userid);