我已经看过两个或更多使用改装2.0来应用帖子的教程。所有这些都和我一样实现。但我得到如下错误
onFailure:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException预期为BEGIN_OBJECT但在第1行第1行为STRING
我想传递我从编辑文本中获取的简单字符串。然后将它发布在我的服务器数据库中...但它只是返回此异常。如何使它正确 我想在服务器端接收数据作为字符串,或者我想以字符串
发送数据Api服务界面
import retrofit.Call;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.POST;
/**
* Created by Shaon on 8/14/2016.
*/
public interface APIService {
@GET("my_json")
Call<List<People>> getPeopleDetails();
@POST("my_json/insert.php")
Call<People> setPeopleDetails(@Body People people);
}
人类
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Shaon on 8/14/2016.
*/
public class People {
private String id = "";
@SerializedName("name")
@Expose
private String name = "";
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我的帖子方法
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://w...content-available-to-author-only...e.org/").
addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
People people = new People();
people.setName(editName.getText().toString());
Call<People> peopleCall = service.setPeopleDetails(people);
peopleCall.enqueue(new Callback<People>() {
@Override
public void onResponse(Response<People> response, Retrofit retrofit) {
hidepDialog();
Log.d("onResponse", "There is an error");
}
@Override
public void onFailure(Throwable t) {
hidepDialog();
Log.d("onFailure", t.toString());
}
});
我的服务器端代码
$id = $_POST["id"];
$name = $_POST["name"];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO mytable (name)
VALUES ('$name')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>