Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive a Json array ("result") with retrofit2

I know how to receive arrays when they are this type:

[
{
    "username": "luis",
    "job": "developer",
    "age": 23
}
]

my problem is when I must receive an array with a specific name like this:

{"result":[{"userid":"1","username":"Luis","job":"developer","age":"23"}]}

in this case I must receive the array above with the name "result" using retrofit2. Can anyone please help me I'm new at Retrofit.

This is what I have tried:

MainActivity

apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
       Call<List<Workers>> call = apiInterface.getWorkers();

       call.enqueue(new Callback<List<Workers>>() {
           @Override
           public void onResponse(Call<List<Workers>> call, Response<List<Workers>> response) {
               list=response.body();
               adapter = new WorkerAdapter(getApplicationContext(),list);
               recyclerView.setAdapter(adapter);


           }

           @Override
           public void onFailure(Call<List<Workers>> call, Throwable t) {

           }
       });

ApiClient:

public class ApiClient {

    public static final String BASE_URL="http://192.168.31.206/test1_database/";
    public static Retrofit retrofit = null;

    public static Retrofit getApiClient(){

        if (retrofit==null){
            retrofit=new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create()).build();
        }
        return retrofit;
    }

}

ApiInterface:

public interface ApiInterface {

    @GET("getAllUser.php")
    Call<List<Workers>> getWorkers();


}

my POJO or Workers class:

public class Workers {

    @SerializedName("username")
    String Name;
    @SerializedName("job")
    String Job;
    @SerializedName("age")
    int Age;

    public String getName() {
        return Name;
    }

    public String getJob() {
        return Job;
    }

    public int getAge() {
        return Age;
    }
} 

and finally my RecyclerAdpter:

public class WorkerAdapter extends RecyclerView.Adapter<WorkerAdapter.ViewHolder>{
    Context context;
    List<Workers> list;

    public WorkerAdapter(Context context,List<Workers> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_recycler,parent,false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.name.setText(list.get(position).getName());
        holder.job.setText(list.get(position).getJob());
        holder.age.setText(String.valueOf(list.get(position).getAge()));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView name, job, age;

        public ViewHolder(View itemView) {
            super(itemView);
            name= (TextView) itemView.findViewById(R.id.nametxt);
            job=(TextView) itemView.findViewById(R.id.jobtxt);
            age=(TextView) itemView.findViewById(R.id.agetxt);
        }
    }
}

I've been stuck for two days now and I still can't solve it. Please help!

like image 584
Luis Aguilar Avatar asked Mar 05 '23 06:03

Luis Aguilar


1 Answers

create a class model called Result and in Interface class write this code:

 @GET("your endpoint")
 Call<Result>getResult();

and in Result class write below code:

 @SerializedName("result") 
 private List<UserInfo> userInfo;

good luck.

like image 181
Arad Sheybak Avatar answered Mar 07 '23 21:03

Arad Sheybak