2023-07-29

How to read response data from a custom type of json in android studio

I have a group of classes in the API, and I want the results of the request to be semi-uniform, for example, I created a class that contains 3 fields as follows below :

 public class resultDataClass
    {
        public Boolean state { get;set; }
        public string message { get; set; }
        public object data { get; set; } //this field 

    }

The third field returns different data from all classes may be type op users class or department class or another class -- below show data json in my response type

my be :

{
"state": true,
"message": " data description",
"data": [
          {
             "id": 1,
             "UserName": "",
             "password": "",
             "email": "",
             "phone": ""
          },
          {
             "id": 2,
             "UserName": "",
             "password": "",
             "email": "",
             "phone": ""
          }
        ]
}

and my be from another class and different fields:

{
"state": true,
"message": " data description",
"data": [
          {
             "id": 1,
             "title": "",
             "content": "",
             "description": "",
             "image": ""
          },
          {
             "id": 1,
             "title": "",
             "content": "",
             "description": "",
             "image": ""
          }
        ]
}

now , How can I build another Unified Class in Android Studio Java to fetch data from this json data

how to set response class this :

public class ResponseClass {
    public boolean state;
    public String message;
    // here how to define object like api class in up 
    public List<??>

    public boolean isState() {
        return state;
    }

    public String getMessage() {
        return message;
    }
}

I am written this code to fetch data for one class .

String myMessage="";
Call<ResponseData> call=api.getPosts(userId);
    call.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(@NonNull Call<ResponseData> call, @NonNull Response<ResponseData> response) {
                    if(response.isSuccessful()) {

                        if(response.body.isstate)
                            {
                             
                        try {
                            for(int i = 0; i< response.body().getData().size(); i++)
                            {
                                //here fetch data from desired class

                                //It could be a user class or any other class
                                mylist.add(row);
                            }
                            
                            adapter=new MyAdapter(getContext(),mylist);
                            recyclerView.setAdapter(adapter);
                            pd.dismiss();
                        }catch (Exception exception)
                        {
                           
                        }
                    } else myMessage=response.body.getmessage();}
                    else {
                        
                    }
        }

        @Override
        public void onFailure(@NonNull Call<ResponseContentData> call, @NonNull Throwable t) {
            Toast.makeText(getContext(), "message :"+ t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();

        }
    });


No comments:

Post a Comment