beginner beginner - 20 days ago 4
Android Question

Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

** Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $**


i have added needed codes below
problems is i just simply get and set response with Gson but it throws error


code

List<TopicDetailData> data = gson.fromJson(response.getJSONObject("data").toString(), new TypeToken<List<TopicDetailData>>() {
}.getType());


TopicDetailData

public class TopicDetailData {
String topic_id;
String tag;
String sub_id;
String title;
String desc;
String video_url;
String video_thumb;
String timestamp;
ArrayList<RelatedData> related;

public ArrayList<RelatedData> getRelated() {
return related;
}

public void setRelated(ArrayList<RelatedData> related) {
this.related = related;
}


public String getTag() {
return tag;
}

public void setTag(String tag) {
this.tag = tag;
}

public String getTopic_id() {
return topic_id;
}

public void setTopic_id(String topic_id) {
this.topic_id = topic_id;
}

public String getSub_id() {
return sub_id;
}

public void setSub_id(String sub_id) {
this.sub_id = sub_id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getVideo_url() {
return video_url;
}

public void setVideo_url(String video_url) {
this.video_url = video_url;
}

public String getVideo_thumb() {
return video_thumb;
}

public void setVideo_thumb(String video_thumb) {
this.video_thumb = video_thumb;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}


}


RelatedData

public class RelatedData {

String topic_id;
String tag;
String sub_id;
String title;
String desc;
String video_url;
String video_thumb;
String timestamp;

public RelatedData() {
}

public String getTag() {
return tag;
}

public void setTag(String tag) {
this.tag = tag;
}

public String getSub_id() {
return sub_id;
}

public void setSub_id(String sub_id) {
this.sub_id = sub_id;
}

public String getTopic_id() {
return topic_id;
}

public void setTopic_id(String topic_id) {
this.topic_id = topic_id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getVideo_url() {
return video_url;
}

public void setVideo_url(String video_url) {
this.video_url = video_url;
}

public String getVideo_thumb() {
return video_thumb;
}

public void setVideo_thumb(String video_thumb) {
this.video_thumb = video_thumb;
}

public String getTimestamp() {
return timestamp;
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}


Json response

{
"status": "success",
"data": {
"topic_id": "2",
"tag": "",
"sub_id": "1",
"title": "TEST",
"desc": "<p>here is multiple option for you</p>\n",
"video_url": "",
"video_thumb": "",
"timestamp": "2016-11-16 04:48:12",
"related": [
{
"topic_id": "2",
"tag": "",
"sub_id": "1",
"title": "TEST",
"desc": "<p>here is multiple option for you</p>\n",
"video_url": "",
"video_thumb": "",
"timestamp": "2016-11-16 04:48:12"
},
{
"topic_id": "3",
"tag": "",
"sub_id": "1",
"title": "TEST",
"desc": "<p>here is multiple option for you</p>\n",
"video_url": "",
"video_thumb": "",
"timestamp": "2016-11-16 04:47:22"
},
{
"topic_id": "4",
"tag": "",
"sub_id": "1",
"title": "TEST",
"desc": "<p>here is multiple option for you</p>\n",
"video_url": "",
"video_thumb": "",
"timestamp": "2016-11-16 04:46:40"
},
{
"topic_id": "5",
"tag": "",
"sub_id": "4",
"title": "check",
"desc": "<p><strong>this ..</strong></p>\n",
"video_url": "",
"video_thumb": "",
"timestamp": "2016-11-16 04:45:08"
}
]
}
}

Answer

You Should try To fetch like below

TopicDetailData topicDetail = gson.fromJson(
    response.getJSONObject("data").toString(),TopicDetailData.class);

this will convert your json response to your pojo class if it matches to json response..... happy Coding