0

That's how the main Activity looks like

      Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiCallInterface apiCallInterface = retrofit.create(ApiCallInterface.class);
        Call<MenuResponse> call = apiCallInterface.getResponseData("eyJhcGlrZXkiOiI1YTRmNzRiNTUyNzUxOWMwYzY3MGMwZWQ5MDRhZTM0MSJ9");

        call.enqueue(new Callback<MenuResponse>() {
            @Override
            public void onResponse(Call<MenuResponse> call, Response<MenuResponse> response) {
                Log.d(TAG, "onResponse: server response" + response.toString());
                Log.d(TAG, "onResponse: received info" + response.body().toString());

                List<Datum> children = response.body().getData();

                for (int i = 0; i < children.size(); i++) {
                    Log.d(TAG, "onresopnse" +
                            "title: " + children.get(i).getTitle());
                }
            }

            @Override
            public void onFailure(Call<MenuResponse> call, Throwable t) {
                Log.d(TAG, "onFailure something went wrong" + t.getMessage());
                Toast.makeText(ContactUsActivity.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
            }
        });

Api Interface kinda looks like this:

    public interface ApiCallInterface {
    @POST("menu")
    Call<MenuResponse> getResponseData(@Query("data") String data);
}

This is my main Model class named: menuresponse

    public class MenuResponse {

    @SerializedName("data")
    private List<Datum> mData;

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

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

from menu response Datum class:

    public class Datum {
    @SerializedName("childs")
    private List<Child> mChilds;
    @SerializedName("ID")
    private Long mID;
    @SerializedName("menu_item_parent")
    private String mMenuItemParent;
    @SerializedName("object")
    private String mObject;
    @SerializedName("object_id")
    private String mObjectId;
    @SerializedName("title")
    private String mTitle;
    @SerializedName("type")
    private String mType;
    @SerializedName("url")
    private String mUrl;

    public List<Child> getChilds() {
        return mChilds;
    }

    public void setChilds(List<Child> childs) {
        mChilds = childs;
    }

    public Long getID() {
        return mID;
    }

    public void setID(Long iD) {
        mID = iD;
    }

    public String getMenuItemParent() {
        return mMenuItemParent;
    }

    public void setMenuItemParent(String menuItemParent) {
        mMenuItemParent = menuItemParent;
    }

    public String getObject() {
        return mObject;
    }

    public void setObject(String object) {
        mObject = object;
    }

    public String getObjectId() {
        return mObjectId;
    }

    public void setObjectId(String objectId) {
        mObjectId = objectId;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getType() {
        return mType;
    }

    public void setType(String type) {
        mType = type;
    }

    public String getUrl() {
        return mUrl;
    }

    public void setUrl(String url) {
        mUrl = url;
    }

}

And from Datum child class looks like this

    public class Child {
    @SerializedName("childs")
    private List<Child> mChilds;
    @SerializedName("ID")
    private Long mID;
    @SerializedName("menu_item_parent")
    private String mMenuItemParent;
    @SerializedName("object")
    private String mObject;
    @SerializedName("object_id")
    private String mObjectId;
    @SerializedName("title")
    private String mTitle;
    @SerializedName("type")
    private String mType;
    @SerializedName("url")
    private String mUrl;

    public List<Child> getChilds() {
        return mChilds;
    }

    public void setChilds(List<Child> childs) {
        mChilds = childs;
    }

    public Long getID() {
        return mID;
    }

    public void setID(Long iD) {
        mID = iD;
    }

    public String getMenuItemParent() {
        return mMenuItemParent;
    }

    public void setMenuItemParent(String menuItemParent) {
        mMenuItemParent = menuItemParent;
    }

    public String getObject() {
        return mObject;
    }

    public void setObject(String object) {
        mObject = object;
    }

    public String getObjectId() {
        return mObjectId;
    }

    public void setObjectId(String objectId) {
        mObjectId = objectId;
    }

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public String getType() {
        return mType;
    }

    public void setType(String type) {
        mType = type;
    }

    public String getUrl() {
        return mUrl;
    }

    public void setUrl(String url) {
        mUrl = url;
    }

    @Override
    public String toString() {
        return "Child{" +
                "mChilds=" + mChilds +
                ", mID=" + mID +
                ", mMenuItemParent='" + mMenuItemParent + '\'' +
                ", mObject='" + mObject + '\'' +
                ", mObjectId='" + mObjectId + '\'' +
                ", mTitle='" + mTitle + '\'' +
                ", mType='" + mType + '\'' +
                ", mUrl='" + mUrl + '\'' +
                '}';
    }
}

So I want to get data from web service and json data has multiple childs within childs so I am totally confused Nd tried many things, but getting same error Nd onFailure so I think that problem is within mainactivity.

My goal is to get title from child class.

Thomas Morris
  • 794
  • 5
  • 26

1 Answers1

0

Edited Answer

If you want to get the title from Child List

List<Child> childList = response.body().getData().get(position).getChilds();

Then you can access title from the childList like that .

String title = childList.get(position).getTitle();

Edit

Accessing title of Child class

               //First we will add child data in childList
                    List<Child> childList = new ArrayList<>();
                    for(int i=0;i< response.body().getData().size();i++){
                        childList.add(response.body().getData().get(i).getChilds());
                    }
                    //for access title in child list
                    for(int i=0;i<childList.size();i++){
                        //we suppose you want to show all title of the child class in Logs
                        Log.e("title"," "+childList.get(i).getTitle());
                    }
Ahmed Elsayed
  • 231
  • 3
  • 23
  • List childList = response.body().getData().get(position).getChilds(); what i should put instead of position?? –  Aug 26 '20 at 13:30
  • It depends on your code . If you want to use this list in for loop then you will replace position with the loop index – Ahmed Elsayed Aug 26 '20 at 13:32
  • If you want to get the first item only for example , then replace position with 0(zero) to access the first position , and so on – Ahmed Elsayed Aug 26 '20 at 13:33
  • Thank you, I applied your edited answer but getting this error java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.xpresshopapp.net.retrofit.MenuResponse.getData()' on a null object reference –  Aug 26 '20 at 14:05
  • Is your response null ?? – Ahmed Elsayed Aug 26 '20 at 14:07
  • See ApiCallInterface class –  Aug 26 '20 at 14:07
  • i am gettig this in log "received infonull" result of response.body() –  Aug 26 '20 at 14:09
  • Can i see a sample of the MenuResponse object as a json ? but before that check if the ``SerializedName`` carry the right name in the json – Ahmed Elsayed Aug 26 '20 at 14:17
  • check this answer and change your pojo classes as shown in this answer https://stackoverflow.com/a/46190758/11567530 – Ahmed Elsayed Aug 26 '20 at 14:31
  • yes SerializedName names are perfect are cross-checked I think the main problem is i have to call MenuResponse in main activity and i want the data from child class. –  Aug 26 '20 at 15:09
  • can you suggest how to get anyone value-form menuResponse or Datum class for seeing that whether it working or not other than that i am getting response of null of response.body() –  Aug 26 '20 at 15:10