【问题标题】:Error in getting gson while using Get Method in Retrofit [duplicate]在 Retrofit 中使用 Get 方法时获取 gson 时出错 [重复]
【发布时间】:2021-11-20 02:34:33
【问题描述】:

我不明白我在 String 中设置的所有内容。但仍然有这个问题。当我测试我的网络 API 时,这件事没有问题.. 但是当我实施时遇到了这个问题..

这是我的错误

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path

这是我调用 get Section 的代码

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemID) {
                if (position >= 0 && position < goodModelArrayList.size()) {
                    String section= goodModelArrayList.get(position).getSection();
                    //getSelectedCategoryData(section);
                    restService.getService().getSection(section, new Callback<VW_AC_Line_>() {
                        @Override
                        public void success(VW_AC_Line_ student, Response response) {
                            Toast.makeText(Viewattendance.this, "success", Toast.LENGTH_LONG).show();
                        }

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

                        }

                    });
                } else {
                    Toast.makeText(Viewattendance.this, "Selected Category Does not Exist!", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

这里是我要调用的数据类


public class VW_AC_Line_ {

    public int ID;
    public String Section;

    public int getID(){
        return ID;
    }

    public void setID(int id){
        this.ID = id;
    }
    public String getSection() {
        return Section;
    }

    public void setSection(String section) {
        this.Section = section;
    }
}

这是我的调用方法web api

    @GET("/api/VW_AC_LINE_/deparment/{section}")
    public void getSection(@Path("section") String section, Callback<VW_AC_Line_> callback);

这里是 web api 调用

[{"ID":2665,"Section":"IT"}].

【问题讨论】:

    标签: java android gson retrofit


    【解决方案1】:

    我认为你得到了低于响应(对象数组)但期待对象。

    对象数组:[{VW_AC_Line_:{ "a":2,"b":3}}]

    预期:{VW_AC_Line_:{ "a":2,"b":3}}

    【讨论】:

    • [{"ID":2665,"Section":"IT"}].. 这是我从我的 web api 得到的
    【解决方案2】:
    restService.getService().getSection(section, new Callback<ArrayList<VW_AC_Line_>>() {
        @Override
        public void success(ArrayList<VW_AC_Line_> student, Response response) {
            Toast.makeText(Viewattendance.this, "success", Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(Viewattendance.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
    
        }
    
    });
    

    将其称为数组列表,因为 Web API 在数组中调用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 2012-12-06
      • 2011-05-08
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      相关资源
      最近更新 更多