0

Hi I am trying to make a changelog list from my REST API to my App, but I have some problems with this error line here:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

The JSON output:

{
    "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"
        }
    ]
}

My REST Call:

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

Here is my Pojo class named: 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;
    }
}

My POJO named: 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);
        }
    }
}

And here is the call from my UpdateActivity class:

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());

}
JonathanNet
  • 1,637
  • 3
  • 15
  • 17

1 Answers1

0

See you are doing a wrong call

your method should be

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

and while handling the response you can do something like

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

and then pass that description Collection object in your adapter. You are calling an List object directly to response but actually the Array of objects is inside an object

MrCurious
  • 150
  • 3
  • 11