在Retrofit [duplicate]中使用Get方法时获取gson时出错

fivyi3re  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(139)

此问题在此处已有答案

GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?(11个答案)
去年关闭了。
我不明白我在字符串中设置的所有内容,但仍然遇到了这个问题。当我测试我的Web API时,没有遇到这个问题。但当我实现时,遇到了这个问题。
这里我的错误

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

下面是调用get部分的代码

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"}].
drnojrws

drnojrws1#

我认为你得到的是下面的响应(对象数组),但期待的是对象。
对象数组:[{VW_AC_行:{“a”:2,“B”:3}}]
预期值:{VW_AC_行:{“a”:2,“B”:3}}

uz75evzq

uz75evzq2#

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调用数组中的

相关问题