如何在改造中使用api获取有限的数据

时间:2019-04-01 13:54:04

标签: android retrofit2

我可以在Android中使用retrofit来获取数据,并且可以使用adapter在recyclerView中设置其视图。但是我不想从我的api中获取整个数据使用Java从我的api中删除一些数据,这些数据在recyclerview中不可见。

Android Studio,Java

private void getSearchProduct(String str_search_text, String page_no, String limit, final String catid) {
    final ProgressDialog progressDialog = new ProgressDialog (this);
    if (limit.equalsIgnoreCase ("")) {
        progressDialog.show ();
        progressDialog.setMessage ("Please wait...");
        progressDialog.setCancelable (false);
    } else {
        progressDialog.setMessage ("Please wait...");
        progressDialog.setCancelable (false);
    }
    if (page_no.equalsIgnoreCase ("")) {
        pagenonew = "";
    } else {
        pagenonew = page_no;
    }
    Log.e (TAG, "getSearchProduct: catid..............." + catid);
    if (catid.equals ("")) {
        category_id = catid;
        Log.e (TAG, "getSearchProduct: catid" + catid);
    }
    apiService = ApiUtils.getAPIService ();
    apiService.getSearchProduct ("" + latitude, "" + longitude, CustomerID, str_search_text, pagenonew, catid).enqueue (new Callback<Search_Product_Model> () {
        @Override
        public void onResponse(Call<Search_Product_Model> call, Response<Search_Product_Model> response) {

            if (response.isSuccessful ()) {
                int status = response.body ().getStatus ();
                String meg = response.body ().getMsg ();
                if (status == 1) {
                    serach_product_lists.clear ();
                    serach_product_lists = response.body ().getProductList ();
                    /*  categoryID = response.body ().getCategory_id ();
                    Log.e (TAG, "categoryID: categoryID......" + categoryID);*/
                    Search_Product_Adapter adapter = new Search_Product_Adapter (SearchProduct_Fragment.this, serach_product_lists);
                    recycler_views_category_offerzone.setAdapter (adapter);
                    txt_noproduct.setVisibility (View.GONE);

                    recycler_views_category_offerzone.setVisibility (View.VISIBLE);
                } else {
                    txt_noproduct.setVisibility (View.VISIBLE);
                    txt_noproduct.setText (meg);
                    recycler_views_category_offerzone.setVisibility (View.GONE);
                }
                progressDialog.dismiss ();
            } else {
                 progressDialog.dismiss ();
            }
        }
        @Override
        public void onFailure(Call<Search_Product_Model> call, Throwable t){
            progressDialog.dismiss ();
        }
    });
}

2 个答案:

答案 0 :(得分:1)

我认为您应该结合使用RxJava和Retrofit(它们确实可以使togheter发挥出惊人的作用),后者具有运算符take,允许您选择要取多少结果

 Observable.just(1, 2, 3, 4, 5, 6, 7, 8)
              .take(4)
              .subscribe(new Subscriber<Integer>() {
            @Override
            public void onNext(Integer item) {
                System.out.println("Next: " + item);
            }

            @Override
            public void onError(Throwable error) {
                System.err.println("Error: " + error.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println("Sequence complete.");
            }
        });

有关更多文档,请查看http://reactivex.io/documentation/operators/take.html

用于结合翻新和RxJAva https://www.journaldev.com/20433/android-rxjava-retrofit

答案 1 :(得分:0)

  1. 询问您的服务提供商不要返回您不想获取的数据。
  2. 遍历数据列表以查找不需要的数据,然后将其删除。请按照以下代码进行操作!

    serach_product_lists = response.body ().getProductList (); for(data:serach_product_lists){ if(//put your judge code here) serach_product_lists.remove(data) }