【问题标题】:retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 5retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但在第 1 行第 5 列为 NUMBER
【发布时间】:2021-11-17 08:22:25
【问题描述】:

这是删除数据的代码

@Override
public void onClick(View v) {
    if (findViewById(R.id.btnDelete)==v){
        restService.getService().deleteEmpById(_Student_Id, new Callback<AC_Daily_Line>() {
            @Override
            public void success(AC_Daily_Line spsTrans, Response response) {
                Toast.makeText(AttendanceDelete.this, "Employee Record Deleted", Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(AttendanceDelete.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();

            }

        });

这是 JSON 数据

{"ID":2603,"AC_ID":"210924-0828","Emp_ID":"123456789","Att_Date":"2021-09-24T08:50:00"}

这是我的数据类

public class AC_Daily_Line {

    public int ID;
    public String AC_ID;
    public String Emp_ID;
    public String Att_Date;
}

这是我的 Web API 调用路径

    @DELETE("/api/AC_Daily_Line/{id}")
    public void deleteEmpById(@Path("id") Integer id,Callback<AC_Daily_Line> callback);

【问题讨论】:

  • 你的数据类/pojo类在哪里??
  • @UmeshYadav 哦,现在我已经更新了我的数据类
  • 遵循最佳实践很有帮助,尤其是在命名约定方面。 Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).。来自:oracle.com/java/technologies/javase/…

标签: android json retrofit


【解决方案1】:

您似乎使用了错误的类型来存储您的 id 变量。在您的 POJO 类中,ID 是一个 int,而从您的 API 检索数据时,您将 ID 作为 Integer 传递。

请注意intInteger 之间存在差异。 int 是原始数据类型,而 Integer 是 Wrapper 类。一个是原始类型,另一个是类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-10
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多