预计BEGIN_ARRAY但是BEGIN_OBJECT json

时间:2017-07-27 16:24:25

标签: java android rest retrofit2 rx-java2

您好我正在尝试从我的REST API到我的应用程序创建一个更改日志列表,但我在这里遇到一些问题:

预计BEGIN_ARRAY,但在第1行第2栏路径为BEGIN_OBJECT $

JSON输出:

{
    "forced": "true",
    "version": "1.0A(125)",
    "description": [
        {
            "description": "Fixed some things 1"
        },
        {
            "description": "Fixed some things 2"
        },
        {
            "description": "Fixed some things 3"
        },
        {
            "description": "Fixed some things 4"
        }
    ]
}

我的REST电话:

@GET("updatecheck")
Observable<List<UpdateCheckResponse>> UpdateCheckChangeLog();

这是我的Pojo类,名为:UpdateCheckResponse.java

public class UpdateCheckResponse {
    @SerializedName("forced")
    @Expose
    private String forced;
    @SerializedName("version")
    @Expose
    private String version;
    @SerializedName("description")
    @Expose
    private List<UpdateCheckDescription> description = null;

    public String getForced() {
        return forced;
    }

    public void setForced(String forced) {
        this.forced = forced;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public List<UpdateCheckDescription> getDescription() {
        return description;
    }

    public void setDescription(List<UpdateCheckDescription> description) {
        this.description = description;
    }
}

我的POJO命名为:UpdateCheckDescription.java

public class UpdateCheckDescription {
    @SerializedName("description")
    @Expose
    private String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

UpdateChangeLogAdapter.class

public class UpdateChangeLogAdapter extends RecyclerView.Adapter<UpdateChangeLogAdapter.ViewHolder> {

    private ArrayList<UpdateCheckDescription> mAndroidList;

    public UpdateChangeLogAdapter(ArrayList<UpdateCheckDescription> androidList) {
        mAndroidList = androidList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_update, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.mTvChangelog.setText(mAndroidList.get(position).getDescription());
    }

    @Override
    public int getItemCount() {
        return mAndroidList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView mTvChangelog;
        public ViewHolder(View view) {
            super(view);

            mTvChangelog = (TextView)view.findViewById(R.id.descriptionTextView);
        }
    }
}

这是来自我的UpdateActivity类的调用:

private void loadJSON() {

    FitnessM8REST apiService =
            FitnessM8RestClient.getClient().create(FitnessM8REST.class);

    mCompositeDisposable.add(apiService.UpdateCheckChangeLog()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(this::handleResponse,this::handleError));
}

private void handleResponse(List<UpdateCheckResponse> androidList) {

    mAndroidArrayList = new ArrayList<>(androidList);
    mAdapter = new UpdateChangeLogAdapter(mAndroidArrayList);
    mRecyclerView.setAdapter(mAdapter);
}

private void handleError(Throwable error) {
    Toast.makeText(this, "Error "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
    Log.d("Error: ", error.getLocalizedMessage());

}

1 个答案:

答案 0 :(得分:0)

看到你正在拨打错误电话

你的方法应该是

@GET("updatecheck")Call<UpdateCheckResponse> methodName();

并且在处理响应时您可以执行类似

的操作
List<UpdateCheckDescription> description;
description = response.body().getDescription();

然后在适配器中传递该描述Collection对象。 您正在直接调用List对象来响应,但实际上对象数组是在对象