Mohammad Nouri Mohammad Nouri - 1 year ago 111
Android Question

Why not show posts from server when use Retrofit in Android

I want to use

to load data from server, and I use
for set and get data.

This is what I would like to do. When I click on a Category, I want to see posts from this category in the other

For showing category posts I use this link :

For filtering I use
category id

For example : by this link i see all of posts from Category1.

Retrofit interface
for set link: (I set
in other class)

public interface Retrofit_ApiInterface {

// For Categories Response
Call<R_CatModelResponse> getCatResponse(@Query("id") Integer id);

I send category id to other activity by this code :

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

private List<Retrofit_ColoniesModel> mDateSet;
private Context mContext;
private SparseBooleanArray expandState = new SparseBooleanArray();

public ColoniesAdapter(Context context, List<Retrofit_ColoniesModel> dataSet) {
this.mContext = context;
this.mDateSet = dataSet;
for (int i = 0; i < mDateSet.size(); i++) {
expandState.append(i, false);

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(mContext).inflate(R.layout.colonies_row, parent, false);
return new ViewHolder(view);

public void onBindViewHolder(final ViewHolder holder, final int position) {

holder.colonies_title.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int pos = holder.getPosition();
Retrofit_ColoniesModel model = mDateSet.get(pos);
mContext.startActivity(new Intent(v.getContext(), Category_page.class)
.putExtra("categoryTitle", model.getTitle())
.putExtra("categoryID", model.getId()));

Toast.makeText(mContext, " " + model.getId(), Toast.LENGTH_SHORT).show();


Category_page code:

public class Category_page extends AppCompatActivity {

private static final long RIPPLE_DURATION = 250;
private Toolbar toolbar;
private TextView toolbar_title;
private ImageView toolbar_menuImage;
private RelativeLayout root;
private CategoryAdapter mAdapter;
private RecyclerView cat_recyclerView;
private LinearLayoutManager mLayoutManager;
private RelativeLayout loadLayout;
private String catTitle = "";
private Integer catID;
private Bundle bundle;
private int pageCount = 1;
private Context context;

protected void onCreate(Bundle savedInstanceState) {
//if (!EventBus.getDefault().isRegistered(this)) {
// EventBus.getDefault().register(this);

// Hide StatusBar color

// Initializing
context = Category_page.this;
toolbar = (Toolbar) findViewById(;
cat_recyclerView = (RecyclerView) findViewById(;
toolbar_title = (TextView) toolbar.findViewById(;
mLayoutManager = new LinearLayoutManager(this);
root = (RelativeLayout) findViewById(;
loadLayout = (RelativeLayout) findViewById(;
// Toolbar
if (toolbar != null) {

// Receive Data
bundle = getIntent().getExtras();
catID = bundle.getInt("categoryID");
if (bundle != null) {
catTitle = bundle.getString("categoryTitle");
if (catTitle != null) {

// Load data
// Menu
View guillotineMenu = LayoutInflater.from(this).inflate(R.layout.menu_layout, null);
toolbar_menuImage = (ImageView) toolbar.findViewById(;
new GuillotineAnimation.GuillotineBuilder(guillotineMenu, guillotineMenu.findViewById(, toolbar_menuImage)
// RecyclerView

// Retrofit //////////
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<R_CatModelResponse> call = apiInterface.getCatResponse(catID);

call.enqueue(new Callback<R_CatModelResponse>() {
public void onResponse(Call<R_CatModelResponse> call, Response<R_CatModelResponse> response) {
List<R_CatModel> models = response.body().getCat_posts();

mAdapter = new CategoryAdapter(context, cat_recyclerView, models);

Toast.makeText(Category_page.this, "Response", Toast.LENGTH_SHORT).show();

public void onFailure(Call<R_CatModelResponse> call, Throwable t) {

Toast.makeText(Category_page.this, "Error", Toast.LENGTH_SHORT).show();


I send category_id with below code from one adapter :

mContext.startActivity(new Intent(v.getContext(), Category_page.class)
.putExtra("categoryTitle", model.getTitle())
.putExtra("categoryID", model.getId()));

and receive this data with below code :

// Receive Data
bundle = getIntent().getExtras();
catID = bundle.getInt("categoryID");

But Error message (
) instead of category posts!

show error toast from :

public void onFailure(Call<R_CatModelResponse> call, Throwable t) {
Toast.makeText(Category_page.this, "Error", Toast.LENGTH_SHORT).show();

Update :

This is the error I get, :

E/CatResponseError: Error : java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 48 path $.category

Update #2 :

Added my POJO class :

public class R_CatModelResponse {

public String Cat_status;
public int Cat_count;
public int Cat_pages;
public List<Retrofit_ColoniesModel> category;
public List<R_CatModel> Cat_posts;

public String getCat_status() {
return Cat_status;

public void setCat_status(String cat_status) {
Cat_status = cat_status;

public int getCat_count() {
return Cat_count;

public void setCat_count(int cat_count) {
Cat_count = cat_count;

public int getCat_pages() {
return Cat_pages;

public void setCat_pages(int cat_pages) {
Cat_pages = cat_pages;

public List<Retrofit_ColoniesModel> getCategory() {
return category;

public void setCategory(List<Retrofit_ColoniesModel> category) {
this.category = category;

public List<R_CatModel> getCat_posts() {
return Cat_posts;

public void setCat_posts(List<R_CatModel> cat_posts) {
Cat_posts = cat_posts;

How can I fix this?

Please help me. Thanks in advance.

Answer Source

See this pattern which make your life easier:D, based on that use following structure:

Your data model should be:

public class Model {

    String status;
    int count;
    int page;
    Category category;
    List<Post> posts;

    // implement rest of thing

    public class Category{

        int id;
        String slug;
        String title;
        String description;
        int parent;
        int post_count;

    public class Post {
        int id;
        String type;
        String slug;
        // of thing


Your service interface should be:

public interface IService {

        Call<Model> getCategoryPost(
                @Query("id") String id

                //...other query if you need should added like below
               @Query("categorySlug") String categorySlug,
               @Query("tag") String tag,

And your ServiceHelper contains following method:

public Call<CategoryModel> getAllCategory() {
    return service.getCateogryPost();

And your error cause your POJO is not fitted to server Model. Double check your Model and make sure Object instead of List/Array;

In your case instead of List<Retrofit_ColoniesModel> category use Retrofit_ColoniesModel category;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download