简体   繁体   English

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

[英]Can't fetch data from API, what am i doing wrong?

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 中的数据填充 recyclerview 来自下面主表中子表的数据都是为了更清晰的代码。

API JSON. API JSON。 i am aware of the problem with expected begin_object but was begin_array and did it properly.我知道预期的 begin_object 存在问题,但它是 begin_array 并且做得正确。

{
  "Status": 200,
  "Message": "OK",
  "Data": {
    "MainServices": [
      {
        "Id": 11,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "تكييفات\r\n",
        "MainServiceNameEn": "Air conditioning\r\n",
        "SubServices": []
      },
      {
        "Id": 12,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "كهرباء\r\n",
        "MainServiceNameEn": "Electricity\r\n",
        "SubServices": []
      },
      {
        "Id": 14,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "سباكة",
        "MainServiceNameEn": "Plumbing\r\n",
        "SubServices": [
          {
            "Id": 24,
            "MainServiceId": 14,
            "SubServiceNameAr": "ادوات صحة\r\n",
            "SubServiceNameEn": "Health Tools\r\n",
            "ServicePriceLists": []
          },
          {
            "Id": 25,
            "MainServiceId": 14,
            "SubServiceNameAr": "انسداد المياه\r\n",
            "SubServiceNameEn": "Water blockage\r\n",
            "ServicePriceLists": []
          }
        ]
      },
      {
        "Id": 15,
        "ServiceTypeId": 1,
        "MainServiceNameAr": "اجهزة منزلية\r\n",
        "MainServiceNameEn": "Home appliances\r\n",
        "SubServices": []
      },
      {
        "Id": 16,
        "ServiceTypeId": 2,
        "MainServiceNameAr": "نظافة\r\n",
        "MainServiceNameEn": "Cleaning\r\n",
        "SubServices": []
      }
    ]
  }
}

Models.楷模。 i separated the models as ResponseModel, DataModel (where DataModel includes the rest of the Lists and services) and MainServicesModel with ServicesTypeModel我将模型分离为 ResponseModel、DataModel(其中 DataModel 包括列表和服务的 rest)和带有 ServicesTypeModel 的 MainServicesModel

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我在 API 中创建了另一个服务以方便我的编码

getMainServicesByServicesTypeId(ServiceTypeId)

any help would be appreciated thank you in advance if u need any more clarification please don't hesitate to ask任何帮助将不胜感激提前谢谢你如果你需要更多的澄清请不要犹豫问

call it at the MainServiceclient在 MainServiceclient 调用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法得到这个编译,我做错了什么? - Can't get this to compile, what am I doing wrong? 我在做什么错 - 无法从 Main(自定义类)实例化 object - What am I doing wrong - can't instantiate object from Main (Custom class) 我试图使用angularjs将数据从servlet提取到jsp中。 但是,ng-click似乎不起作用。 我究竟做错了什么? - I am trying to fetch data from a servlet into a jsp using angularjs. However, the ng-click seems to be not working. What am I doing wrong? 我究竟做错了什么?? - What am I doing wrong?? 我做错了什么 - What am i doing wrong with this 空指针异常,我无法弄清楚我在做什么错 - Null Pointer Exception, I can't figure out what I am doing wrong 无法打印年薪。 我究竟做错了什么? - Can't get Annual Salary to Print. What am I doing wrong? Java Udemy 课程闰年计算器 - 无法弄清楚我做错了什么 - Java Udemy course leap year calculator - can't figure out what am I doing wrong 无法弄清楚我在此方法中做错了什么(compute_even) - Can't figure out what I am doing wrong in this method ( compute_even ) 我在 Java 中的方法找不到变量的值并将其设置为 0。我做错了什么? - My method in Java can't find the value of the variable and sets it as 0. What am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM