Shadab Khan Shadab Khan - 17 days ago 6
JSON Question

Nested Json using retrofit

i want to read sub_category > id, name from json using retrofit

But below code give failed response.


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


My Json....

[
{
"id": "1",
"name": "Invamore",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "102",
"name": "3 sided Dangler"
},
{
"id": "103",
"name": "Leaflet"
}
]
},
{
"id": "2",
"name": "HUPS",
"sub_category": [
{
"id": "103",
"name": "Leaflet"
},
{
"id": "104",
"name": "Posters"
},
{
"id": "105",
"name": "Sunpack"
}
]
},
{
"id": "3",
"name": "Xplore",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "103",
"name": "Leaflet"
},
{
"id": "106",
"name": "Dangler"
},
{
"id": "107",
"name": "Vertical Streamer"
}
]
},
{
"id": "4",
"name": "Xpress",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "103",
"name": "Leaflet"
},
{
"id": "108",
"name": "Streamer"
}
]
},
{
"id": "5",
"name": "Matrix",
"sub_category": [
{
"id": "103",
"name": "Leaflet"
}
]
},
{
"id": "6",
"name": "Instabrite",
"sub_category": [
{
"id": "103",
"name": "Leaflet"
}
]
},
{
"id": "7",
"name": "Mileage",
"sub_category": [
{
"id": "107",
"name": "Vertical Streamer"
}
]
},
{
"id": "8",
"name": "Onam Posm",
"sub_category": [
{
"id": "101",
"name": "Banner"
},
{
"id": "106",
"name": "Dangler"
}
]
}]


Backend.java

public void pos_func(String user_id) {

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

Call call = apiService.POS_MODEL_CALL(user_id);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {

}

@Override
public void onFailure(Call call, Throwable t) {
Log.d("sk_log", "Failed! Error = " + t.getMessage());

}
});

}


ApiInterface.java

@FormUrlEncoded
@POST("pos_distributed.php")
Call<ValuesPos> POS_MODEL_CALL(@Field("user_id") String user_id);


Model : from pojoschema2pojo.com for above JSON

ValuesPos.java

package com.example.shkhan.myapp.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

/**
* Created by shkhan on 14/11/16.
*/
public class ValuesPos {
@SerializedName("id")
@Expose
public String id;
@SerializedName("name")
@Expose
public String name;
@SerializedName("sub_category")
@Expose
public List<SubCategory> subCategory = new ArrayList<SubCategory>();

/**
*
* @return
* The id
*/
public String getId() {
return id;
}

/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}

/**
*
* @return
* The name
*/
public String getName() {
return name;
}

/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}

/**
*
* @return
* The subCategory
*/
public List<SubCategory> getSubCategory() {
return subCategory;
}

/**
*
* @param subCategory
* The sub_category
*/
public void setSubCategory(List<SubCategory> subCategory) {
this.subCategory = subCategory;
}

}


SubCategory.json

package com.example.shkhan.myapp.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
* Created by shkhan on 14/11/16.
*/


public class SubCategory {

@SerializedName("id")
@Expose
public String id;
@SerializedName("name")
@Expose
public String name;

/**
*
* @return
* The id
*/
public String getId() {
return id;
}

/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}

/**
*
* @return
* The name
*/
public String getName() {
return name;
}

/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}

}

Answer

The Json contains a list of ValuesPos

The ApiInterface.java call for a single ValuesPos


EDIT

Change the ApiInterface.java to call for a list of ValuesPos

@FormUrlEncoded
@POST("pos_distributed.php")
Call<List<ValuesPos>> POS_MODEL_CALL(@Field("user_id") String user_id);
Comments