[SOLVED] Can’t fetch data from API, what am i doing wrong?


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

hello iam trying to populate a recyclerview with data from API the data from a sub table inside the main table below is all code for more clarity.

API JSON. i am aware of the problem with expected begin_object but was begin_array and did it properly.

{
  "Status": 200,
  "Message": "OK",
  "Data": {
    "MainServices": [
      {
        "Id": 11,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "تكييفاتrn",
        "MainServiceNameEn": "Air conditioningrn",
        "SubServices": []
      },
      {
        "Id": 12,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "كهرباءrn",
        "MainServiceNameEn": "Electricityrn",
        "SubServices": []
      },
      {
        "Id": 14,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "سباكة",
        "MainServiceNameEn": "Plumbingrn",
        "SubServices": [
          {
            "Id": 24,
            "MainServiceId": 14,
            "SubServiceNameAr": "ادوات صحةrn",
            "SubServiceNameEn": "Health Toolsrn",
            "ServicePriceLists": []
          },
          {
            "Id": 25,
            "MainServiceId": 14,
            "SubServiceNameAr": "انسداد المياهrn",
            "SubServiceNameEn": "Water blockagern",
            "ServicePriceLists": []
          }
        ]
      },
      {
        "Id": 15,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "اجهزة منزليةrn",
        "MainServiceNameEn": "Home appliancesrn",
        "SubServices": []
      },
      {
        "Id": 16,
        "ServiceTypeId": 2,
        "MainServiceNameAr": "نظافةrn",
        "MainServiceNameEn": "Cleaningrn",
        "SubServices": []
      }
    ]
  }
}

Models. i separated the models as ResponseModel, DataModel (where DataModel includes the rest of the Lists and services) and MainServicesModel with ServicesTypeModel

public class MainServicesModel {
    @SerializedName("Id")
    public int Id;
    @SerializedName("ServiceTypeId")
    public int ServiceTypeId;
    @SerializedName("MainServiceNameAr")
    public String MainServiceNameAr;
    @SerializedName("MainServiceNameEn")
    public String MainServiceNameEn;
    @SerializedName("SubServices")
    public List<SubServicesModel> subServicesModel;

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public int getServiceTypeId() {
        return ServiceTypeId;
    }

    public void setServiceTypeId(int serviceTypeId) {
        ServiceTypeId = serviceTypeId;
    }

    public String getMainServiceNameAr() {
        return MainServiceNameAr;
    }

    public void setMainServiceNameAr(String mainServiceNameAr) {
        MainServiceNameAr = mainServiceNameAr;
    }

    public String getMainServiceNameEn() {
        return MainServiceNameEn;
    }

    public void setMainServiceNameEn(String mainServiceNameEn) {
        MainServiceNameEn = mainServiceNameEn;
    }

    public List<SubServicesModel> getSubServicesModel() {
        return subServicesModel;
    }

    public void setSubServicesModel(List<SubServicesModel> subServicesModel) {
        this.subServicesModel = subServicesModel;
    }
} 

MainServiceAdapter

public class MainServiceAdapter extends RecyclerView.Adapter<MainServiceAdapter.MainServiceHolder> {
    private List<MainServicesModel> mainServicesModelList;
    private MainServiceListener listener;


    public MainServiceAdapter(List<MainServicesModel> mainServicesModels , MainServiceListener listener) {
        this.mainServicesModelList = mainServicesModels;
        this.listener = listener;
    }

    @NonNull
    @Override
    public MainServiceAdapter.MainServiceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View iteView = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_service_item, parent, false);
        return new MainServiceHolder(iteView);
    }

    @Override
    public void onBindViewHolder(@NonNull MainServiceAdapter.MainServiceHolder holder, int position) {
        holder.MainServiceTxt.setText(mainServicesModelList.get(position).getMainServiceNameAr());
    }

    @Override
    public int getItemCount() {
//        return null!=mainServicesModelList?mainServicesModelList.size():0;
        return mainServicesModelList.size();
    }

    public long getItemId(int position) {
        return position;
    }

    public void setMainServicesModelList(List<MainServicesModel> mainServicesModelList) {
        this.mainServicesModelList = mainServicesModelList;
        notifyDataSetChanged();
    }

    public class MainServiceHolder extends RecyclerView.ViewHolder {
        TextView MainServiceTxt;
//        ImageView
        public MainServiceHolder(@NonNull View itemView) {
            super(itemView);
            MainServiceTxt = itemView.findViewById(R.id.MainServiceName);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onMainServiceSelected(mainServicesModelList.get(getAdapterPosition()));
                }
            });
        }
    }
    public interface MainServiceListener {
        void onMainServiceSelected(MainServicesModel mainServicesModel);
    }
}

MainActivity


public class MainServicesAndSubServices extends AppCompatActivity implements MainServiceAdapter.MainServiceListener {

    private final List<MainServicesModel> mainServicesModelArrayList = new ArrayList<>();
    MainServiceAdapter serviceAdapter = new MainServiceAdapter(mainServicesModelArrayList, this);
    MainServiceViewModel mainServiceViewModel;
    RecyclerView MainServiceRV;
    int ServiceTypeId ;

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

// Here i call the ServiceTypeId from MainServiceType

        Intent intent = getIntent();
        Bundle bd = intent.getExtras();
        if (bd != null) {
            ServiceTypeId = (int) bd.get("ServiceTypeId");
        }
        Toast.makeText(MainServicesAndSubServices.this, "" + ServiceTypeId, 
Toast.LENGTH_SHORT).show();

// Here i call the ServiceTypeId from MainServiceType

        try {
            MainServiceRV = findViewById(R.id.RVMainServices);
            mainServiceViewModel = ViewModelProviders.of(this).get(MainServiceViewModel.class);
            mainServiceViewModel.getMainServicesByServiceTypeId(ServiceTypeId);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            MainServiceRV.setLayoutManager(layoutManager);
            MainServiceRV.setAdapter(serviceAdapter);
            FillMainServicesList();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void FillMainServicesList() {
        mainServiceViewModel.MainServiceMutableLiveData.observe(MainServicesAndSubServices.this, new Observer<ResponseModel>() {
            @Override
            public void onChanged(ResponseModel responseModel) {
                serviceAdapter.setMainServicesModelList(responseModel.getData().getMainServicesModels());
serviceAdapter.notifyDataSetChanged();
                Toast.makeText(MainServicesAndSubServices.this, "" + responseModel.getData().getMainServicesModels(), Toast.LENGTH_SHORT).show();
                MainServiceRV.setAdapter(serviceAdapter);
            }
        });
    }

    @Override
    public void onMainServiceSelected(MainServicesModel mainServicesModel) {

    }
}

I created another service in the API to facilitate my coding

getMainServicesByServicesTypeId(ServiceTypeId)

any help would be appreciated thank you in advance if u need any more clarification please don’t hesitate to ask

Solution

call it at the MainServiceclient

    public Call<ResponseModel> GetMainServiceByServiceTypeId(int id) {
        return serviceMainInterface.GetMainServiceByServiceTypeId(id);
    }

Answered By – Mohammad khaled

people found this article helpful. What about you?