Home > other >  Parsing nested JSON Object in recyclerview using retrofit gives error: "Expected begin ARRAY bu
Parsing nested JSON Object in recyclerview using retrofit gives error: "Expected begin ARRAY bu

Time:01-30

So i was trying to parse JSON data into my recyclerview. But got error saying: "Expected begin ARRAY but was BEGIN_OBJECT at line 1"

My JSON response:

{
"status": "success",
"data": {
    "0": {
        "id": "8",
        "name": "hmhshwv",
        "description": "vhhh",
        "location": "nhhjj"
    },
    "1": {
        "id": "54",
        "name": "mabwb",
        "description": "ywg",
        "location": "gwgw"
    }
    "2": {
        "id": "48",
        "name": "nnf",
        "description": "gwb",
        "location": "wggw"
    }
}

}

My Model class:

public class DataModel {

private String id;
private String firstname;
private String lastname;
private String contact;


public DataModel(String id, String firstname, String lastname, String contact) {
    this.id = id;
    this.firstname = firstname;
    this.lastname = lastname;
    this.contact = contact;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

}

My apiInterface: the api has body parameters such as apikey, userid and storeid.

public interface MyApiCall {

@Headers("Accept: application/json")
@POST("mybiz/api/staff-list")
Call<List<DataModel>> getDataModel(@Body String apikey, @Body String userid,
                                   @Body String storeid);

}

My RetrofitClient class:

public class RetrofitClient {

private static final String BASE_URL = "https://api.pidu.in/";
private static Retrofit retrofit = null;
public static MyApiCall getRetrofitClient() {
    if(retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit.create(MyApiCall.class);
}

}

My Mainactivity:

private static final String TAG = MainActivity.class.getSimpleName();
private static final String apikey = "somethinggg";
private static final String userid = "a number";
private static final String storeid = "158";

RecyclerView recyclerView;
ProgressBar progressBar;
LinearLayoutManager linearLayoutManager;
DataModelAdapter dataModelAdapter;
List<DataModel> dataModels = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    progressBar = (ProgressBar) findViewById(R.id.progressBar3);
    linearLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(linearLayoutManager);
    dataModelAdapter = new DataModelAdapter(dataModels);
    recyclerView.setAdapter(dataModelAdapter);

    fetchDataModel();

}

private void fetchDataModel() {

    progressBar.setVisibility(View.VISIBLE);
    RetrofitClient.getRetrofitClient().getDataModel(apikey, userid, storeid).enqueue(new Callback<List<DataModel>>() {

        @Override
        public void onResponse(@NonNull Call<List<DataModel>> call, @NonNull Response<List<DataModel>> response) {
            if(response.isSuccessful() && response.body() != null) {

                Log.e(TAG, "Error: "   response.body());
                dataModels.addAll(response.body());
                dataModelAdapter.notifyDataSetChanged();
                progressBar.setVisibility(View.GONE);

            }
        }

        @Override
        public void onFailure(@NonNull Call<List<DataModel>> call, @NonNull Throwable t) {
            Toast.makeText(MainActivity.this, "Error: "   t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

}

And for reference my hoppscotch api looks like:

hoppscotch api

I understand that i should parse the JSON object but can't find a particulary way to do that.

CodePudding user response:

Your getDataModel() is requesting a List<DataModel>, but the server's response is returning an Object.

You should create a new class called Response for example, like this:

public class Response {

   private String status;
   private Map<String, DataModel> data;

   public Response(String status, Map<String, DataModel> data) {
      this.status = status;
      this.data = data;
   }

   public String getStatus() {
      return status;
   }

   public void setStatus(String status) {
      this.status = status;
   }

   public Map<String, DataModel> getData() {
      return data;
   }

   public void setData(Map<String, DataModel> data) {
      this.data = data;
   }
}

And your request becomes like this:

public interface MyApiCall {

    @Headers("Accept: application/json")
    @POST("mybiz/api/staff-list")
    Call<List<Response>> getDataModel(
        @Body String apikey, 
        @Body String userid,   
        @Body String storeid
    );
}

CodePudding user response:

I think your JSON format is not valid. You can't handle a list of objects in Object. It's should be a list. Like that:

{
"status": "success",
"data": [
   {
        "id": "8",
        "name": "hmhshwv",
        "description": "vhhh",
        "location": "nhhjj"
    },
     {
        "id": "54",
        "name": "mabwb",
        "description": "ywg",
        "location": "gwgw"
    },
     {
        "id": "48",
        "name": "nnf",
        "description": "gwb",
        "location": "wggw"
    }
  ]
}

And the data model class is:

import java.util.List;

public class DataModel {

    private String status;
    private List<Datum> data = null;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }

}
class Datum {
    private String id;
    private String name;
    private String description;
    private String location;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}
  •  Tags:  
  • Related