无法从API获取数据,我做错了什么?

0md85ypi  于 2022-09-21  发布在  Android
关注(0)|答案(1)|浏览(194)

你好,我正在尝试用API中的数据填充回收器视图,下面主表中的子表中的数据都是代码,以便更清晰。

JSON接口。我知道预期的BEGIN_OBJECT存在这个问题,但它是Begin_ARRAY,并且做得很正确。

{
  "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": []
      }
    ]
  }
}

模特们。我将模型分离为ResponseModel、DataModel(其中DataModel包括其余的列表和服务)和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;
    }
}

主服务适配器

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);
    }
}

主活性

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) {

    }
}

我在API中创建了另一个服务来简化我的编码

getMainServicesByServicesTypeId(ServiceTypeId)

如有任何帮助,我们将不胜感激。如果您需要更多的澄清,请不要犹豫,尽管开口。

gajydyqb

gajydyqb1#

在MainServiceclient呼叫它

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

相关问题