0

i got error java.lang.illegalstateException:Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 42 when Call API with retrofit, here is my code :

Here is my interface :

@FormUrlEncoded
@POST("myCollection")
Call<APIResponse<List<MyCollection>>> getMyCollection(
        @Field("caller_id") String caller_id
);

Here is for Call :

public class UploadVideoToneActivity extends BaseActivity{
List<MyCollection> collection = new ArrayList<>();

 private void loadCollection() {

    ContactItem callerID = SessionManager.getProfile(this);
    Call<APIResponse<MyCollection>> call = ServicesFactory.getService().getMyCollection(callerID.caller_id);
    call.enqueue(new Callback<APIResponse<MyCollection>>() {
        @Override
        public void onResponse(Call<APIResponse<MyCollection>> call, Response<APIResponse<MyCollection>> response) {
            if (response.isSuccessful() && response.body().isSuccessful()) {
                List<MyCollection> data = (List<MyCollection>) response.body().data;
                if (data != null) {
                    collection.clear();
                    collection.addAll(data);
                    rvCollection.getAdapter().notifyDataSetChanged();
                }
            }
            else {
                Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<APIResponse<MyCollection>> call, Throwable t) {
            Toast.makeText(UploadVideoToneActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

Here is The format JSON response from API, Here is The format JSON response from API,

{
 "code": 200,
 "error_message": null,
 "data": [
    [
        {
            "caller_id": "44",
            "content_id": "003",
            "alias": "Eli Sugigi",
            "judul": "Angkat sekarang juga",
            "source_content": "http://bla.bla.mp4",
            "thumb_pic":  "http://bla.bla.mp4",
            "sub_start": "2017-12-27 14:17:10",
            "sub_end": "2018-01-03 00:00:00"
        }
    ],
    [
        {
            "caller_id": "44",
            "content_id": "002",
            "alias": "Eli Sugigi",
            "judul": "Mas Ganteng Angkat Dong",
            "source_content":"source_content": "http://bla.bla.mp4",
            "thumb_pic":  "http://bla.bla.mp4",
            "thumb_pic":"source_content": "http://bla.bla.mp4",
            "thumb_pic":  "http://bla.bla.mp4",
            "sub_start": "2017-12-27 15:52:40",
            "sub_end": "2018-01-03 00:00:00"
        }
     ]
    ]
   }

And here is MyCollection class :

public class MyCollection implements Parcelable {
@SerializedName("content_id")
@Expose
public String content_id;
@SerializedName("alias")
@Expose
public String alias ;
@SerializedName("judul")
@Expose
public String judul;
@SerializedName("source_content")
@Expose
public String source_content;
@SerializedName("thumb_pic")
@Expose
public String thumb_pic;
@SerializedName("sub_start")
@Expose
public String sub_start ;
@SerializedName("sub_end")
@Expose
public String sub_end ;



protected MyCollection(Parcel in) {
    content_id = in.readString();
    alias = in.readString();
    judul = in.readString();
    source_content = in.readString();
    thumb_pic = in.readString();
    sub_start = in.readString();
    sub_end = in.readString();
}

public static final Creator<MyCollection> CREATOR = new Creator<MyCollection>() {
    @Override
    public MyCollection createFromParcel(Parcel in) {
        return new MyCollection(in);
    }

    @Override
    public MyCollection[] newArray(int size) {
        return new MyCollection[size];
    }
};

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(content_id);
    parcel.writeString(alias);
    parcel.writeString(judul);
    parcel.writeString(source_content);
    parcel.writeString(thumb_pic);
    parcel.writeString(sub_start);
    parcel.writeString(sub_end);
}
}
2
  • Show your MyCollection Class Commented Dec 28, 2017 at 6:29
  • Show your ApiResponse<MyCollection> class Commented Dec 28, 2017 at 6:32

3 Answers 3

1

First of all json you provided was incorrect :

This is correct json :

{
    "code": 200,
    "error_message": null,
    "data": [{
            "caller_id": "44",
            "content_id": "003",
            "alias": "Eli Sugigi",
            "judul": "Angkat sekarang juga",
            "source_content": "http://bla.bla.mp4",
            "thumb_pic": "http://bla.bla.mp4",
            "sub_start": "2017-12-27 14:17:10",
            "sub_end": "2018-01-03 00:00:00"
        },
        {
            "caller_id": "44",
            "content_id": "002",
            "alias": "Eli Sugigi",
            "judul": "Mas Ganteng Angkat Dong",
            "source_content": "http://bla.bla.mp4",
            "thumb_pic": "http://bla.bla.mp4",
            "sub_start": "2017-12-27 15:52:40",
            "sub_end": "2018-01-03 00:00:00"
        }
    ]
}

Later for your response class use this as response class:

public class MyResponseClass
{
    private String error_message;

    private ArrayList<Data> data;

    private String code;

    public String getError_message ()
    {
        return error_message;
    }

    public void setError_message (String error_message)
    {
        this.error_message = error_message;
    }

    public  ArrayList<Data> getData ()
    {
        return data;
    }

    public void setData ( ArrayList<Data> data)
    {
        this.data = data;
    }

    public String getCode ()
    {
        return code;
    }

    public void setCode (String code)
    {
        this.code = code;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [error_message = "+error_message+", data = "+data+", code = "+code+"]";
    }
}

Later use this MyResponseData as your retrofit data parser :

ContactItem callerID = SessionManager.getProfile(this);
    Call<MyResponseData> call = ServicesFactory.getService().getMyCollection(callerID.caller_id);

and in your onResponse :

@Override
        public void onResponse(Call<MyResponseData> call, Response<MyResponseData> response) {
            if (response.isSuccessful() && response.body().isSuccessful()) {
                //here collection will be arraylist of MyCollection class
                    collection.clear();
                    collection.addAll(response.body().getData());
                    rvCollection.getAdapter().notifyDataSetChanged();

            }
            else {
                Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
            }
        }

Test it and make minor changes if needed

Sign up to request clarification or add additional context in comments.

4 Comments

thanks ABDevelopers , so "data": [ [ double array is cannot be applied?
No it is invalida json first of all
Inside array you can have multiple objects [{},{}] and if you want nested arrays than [ { [ { } ] },{ [ { } ] } ]
how if my json like this "data": [ { [ { now i can't get data inside here (in 2nd array) } ] } ]
1

The problem is you are getting response in 'JSONArray' and you are trying to receive in JSONObject.

You should use 'List' as return type of getMyCollection(callerID.caller_id) method. Then implement the method.

In onResponse method you have to get list like below

 List<MyCollection> data = (List<MyCollection>) response.body()

Update

You can visit here (http://pojo.sodhanalibrary.com/)

put your JSON response and press Submit. You will get Pojo class structure.

4 Comments

Thanks Asish, i allready change my code in onResponse, but still got same error.. :( do you have another solution?
The 'JSON' response you provided is not valid, please share exact response.
please check Json above, i already update with the new one.. :)
@aqsa you previous code was correct. Just add ApiResponse<MyCollection> class
1

Try changing from APIResponse<MyCollection> to APIResponse<List<MyCollection>>

So the code becomes:

public class UploadVideoToneActivity extends BaseActivity{
List<MyCollection> collection = new ArrayList<>();

 private void loadCollection() {

    ContactItem callerID = SessionManager.getProfile(this);
    Call<APIResponse<List<MyCollection>>> call = ServicesFactory.getService().getMyCollection(callerID.caller_id);
    call.enqueue(new Callback<APIResponse<List<MyCollection>>>() {
        @Override
        public void onResponse(Call<APIResponse<List<MyCollection>>> call, Response<APIResponse<List<MyCollection>>> response) {
            if (response.isSuccessful() && response.body().isSuccessful()) {
                List<MyCollection> data = (List<MyCollection>) response.body().data;
                if (data != null) {
                    collection.clear();
                    collection.addAll(data);
                    rvCollection.getAdapter().notifyDataSetChanged();
                }
            }
            else {
                Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<APIResponse<List<MyCollection>>> call, Throwable t) {
            Toast.makeText(UploadVideoToneActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

Also update your Json(part) to:

"data": [{
            "caller_id": "44",
            "content_id": "003",
            "alias": "Eli Sugigi",
            "judul": "Angkat sekarang juga",
            "source_content": "http://bla.bla.mp4",
            "thumb_pic": "http://bla.bla.mp4",
            "sub_start": "2017-12-27 14:17:10",
            "sub_end": "2018-01-03 00:00:00"
        },
        {
            "caller_id": "44",
            "content_id": "002",
            "alias": "Eli Sugigi",
            "judul": "Mas Ganteng Angkat Dong",
            "source_content": "http://bla.bla.mp4",
            "thumb_pic": "http://bla.bla.mp4",
            "sub_start": "2017-12-27 15:52:40",
            "sub_end": "2018-01-03 00:00:00"
        }
    ]

Hope it helps!

2 Comments

how if my json like this "data": [ { [ { now i can't get data inside here (in 2nd array)} ] }
If you keep your json like: [ { [ { then the second array is considered totally different from first and you might have to specify different keys to each JsonArray to fetch appropriate data; like "data": { "key1":[ { } ], "key2" : [ { } ] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.