【问题标题】:I can't display the data from second Request URL to attached the first request URL in volley我无法显示来自第二个请求 URL 的数据以在 volley 中附加第一个请求 URL
【发布时间】:2016-06-03 11:12:52
【问题描述】:

我正在使用 Recycler View 来显示项目,但我使用两个请求 URL 来通过 volley 获取数据。 我试过但效果不佳,无法通过 volley 从 JSON URL 获取第二次获取数据。

型号:

public class CartItemoriginal {
public String productimg;
public String productname;
public String aliasname;
public int qty;
public String price;
public String delivery;
public String shippincharge;
public String sellername;
public String productid;
private int Quantity;

public CartItemoriginal(String productimg, String productname, String aliasname, int qty, String price, String delivery, String shippincharge, String sellername, String productid) {
    this.productimg = productimg;
    this.productname = productname;
    this.aliasname = aliasname;
    this.qty = qty;
    this.price = price;
    this.delivery = delivery;
    this.shippincharge = shippincharge;
    this.sellername = sellername;
    this.productid = productid;
}

public CartItemoriginal(String productimg, String productname, String aliasname, int qty, String price, String productid) {
    this.productimg = productimg;
    this.productname = productname;
    this.aliasname = aliasname;
    this.qty = qty;
    this.price = price;
    this.productid = productid;

}
public CartItemoriginal(String delivery, String shippincharge, String sellername) {
    this.delivery = delivery;
    this.shippincharge = shippincharge;
    this.sellername = sellername;
}


public int increaseQuantity(int quantity) {
    if (quantity < 1)
        quantity = 1;
    else
        quantity++;

    Quantity = quantity;
    return Quantity;
}

public String getProductimg() {
    return productimg;
}

public void setProductimg(String productimg) {
    this.productimg = productimg;
}

public String getProductname() {
    return productname;
}

public void setProductname(String productname) {
    this.productname = productname;
}

public String getAliasname() {
    return aliasname;
}

public void setAliasname(String aliasname) {
    this.aliasname = aliasname;
}

public int getQty() {
    return qty;
}

public void setQty(int qty) {
    this.qty = qty;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getDelivery() {
    return delivery;
}

public void setDelivery(String delivery) {
    this.delivery = delivery;
}

public String getShippincharge() {
    return shippincharge;
}

public void setShippincharge(String shippincharge) {
    this.shippincharge = shippincharge;
}

public String getSellername() {
    return sellername;
}

public void setSellername(String sellername) {
    this.sellername = sellername;
}

public String getProductid() {
    return productid;
}

public void setProductid(String productid) {
    this.productid = productid;
}

}

适配器:

public class CartlistAdapter extends RecyclerView.Adapter < CartlistAdapter.ViewHolder > {

     private ArrayList < CartItemoriginal > cartlistadp;
     DisplayImageOptions options;
     private Context context;
     public static final String MyPREFERENCES = "MyPrefs";
     SharedPreferences.Editor editor;
     SharedPreferences shared;
     String pos;
     private static final int VIEW_TYPE_ONE = 1;
     private static final int VIEW_TYPE_TWO = 2;



     public CartlistAdapter(ArrayList < CartItemoriginal > cartlistadp, Context context) {

         this.cartlistadp = cartlistadp;
         this.context = context;
         options = new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).showImageOnLoading(R.drawable.b2)
             .showImageForEmptyUri(R.drawable.b2).build();
         if (YelloPage.imageLoader.isInited()) {
             YelloPage.imageLoader.destroy();
         }
         YelloPage.imageLoader.init(ImageLoaderConfiguration.createDefault(context));


     }
     public CartlistAdapter() {

     }


     public int getItemViewType(int position) {

         if (cartlistadp.size() == 0) {
             Toast.makeText(context, String.valueOf(cartlistadp), Toast.LENGTH_LONG).show();
             return VIEW_TYPE_TWO;

         }
         return VIEW_TYPE_ONE;
     }


     @Override
     public CartlistAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

         ViewHolder viewHolder = null;
         switch (position) {
             case VIEW_TYPE_TWO:
                 View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_cart, viewGroup, false);
                 viewHolder = new ViewHolder(view2);
                 // return view holder for your placeholder
                 break;
             case VIEW_TYPE_ONE:
                 View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cartitemrow, viewGroup, false);
                 viewHolder = new ViewHolder(view);
                 // return view holder for your normal list item
                 break;
         }
         return viewHolder;


     }

     @Override
     public void onBindViewHolder(CartlistAdapter.ViewHolder viewHolder, int position) {


         viewHolder.productnames.setText(cartlistadp.get(position).getProductname());
         viewHolder.cartalisname.setText(cartlistadp.get(position).getAliasname());
         viewHolder.cartprice.setText(cartlistadp.get(position).getPrice());
         viewHolder.cartdelivery.setText(cartlistadp.get(position).getDelivery());
         viewHolder.cartshippin.setText(cartlistadp.get(position).getShippincharge() + "%" + " " + "OFF");
         viewHolder.cartsellername.setText(cartlistadp.get(position).getSellername());
         //  viewHolder.qty.setText(cartlistadp.get(position).getQty());
         // viewHolder.wishrating.setText(WishListadp.get(i).getCartitemname());

         YelloPage.imageLoader.displayImage(cartlistadp.get(position).getProductimg(), viewHolder.cartitemimg, options);



     }

     @Override
     public int getItemCount() {
         return cartlistadp.size();
     }
     public long getItemId(int position) {
         return position;
     }


     public class ViewHolder extends RecyclerView.ViewHolder {
         private TextView productnames, cartalisname, cartprice, cartdelivery, cartshippin, cartsellername;
         private ImageView cartitemimg;
         private ImageButton wishbtn, removebtn;
         private CardView cd;
         private EditText qty;

         public ViewHolder(final View view) {
             super(view);

             productnames = (TextView) view.findViewById(R.id.cartitemname);
             cartalisname = (TextView) view.findViewById(R.id.cartalias);
             cartprice = (TextView) view.findViewById(R.id.CartAmt);
             cartdelivery = (TextView) view.findViewById(R.id.cartdel);
             cartshippin = (TextView) view.findViewById(R.id.shippingcrg);
             cartsellername = (TextView) view.findViewById(R.id.cartSellerName);
             cartitemimg = (ImageView) view.findViewById(R.id.cartimg);
             qty = (EditText) view.findViewById(R.id.quantity);


             cd = (CardView) view.findViewById(R.id.cv);
             productnames.setSingleLine(false);
             productnames.setEllipsize(TextUtils.TruncateAt.END);
             productnames.setMaxLines(2);

         }



     }



 }

代码:

public void firstServiceCall(String list) {
      url = Constants.singleproducturl + list;
      showpDialog();
      JsonObjectRequest jsonObjReq = new JsonObjectRequest(
          Request.Method.GET, url, null,
          new Response.Listener < JSONObject > () {
              @Override
              public void onResponse(JSONObject response) {
                  if (response != null) {
                      //   int status=jsonObject.optInt("status");
                      String status = response.optString("status");
                      if (status.equalsIgnoreCase("200")) { //check the status 200 or not
                          try {
                              productpath = response.getString("productPath");
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                          try {
                              JSONObject responses = response.getJSONObject("response");
                              jsonarray = responses.getJSONArray(DATA);

                              if (jsonarray.length() > 0) {
                                  // looping through json and adding to movies list
                                  for (int i = 0; i < jsonarray.length(); i++) {
                                      JSONObject product = jsonarray.getJSONObject(i);
                                      cartpid = product.getString("product_id");
                                      cartproductname = product.getString("product_name");
                                      cartaliasname = product.getString("product_alias");
                                      cartprice = product.getString("mrp_price");
                                      sellerid = product.getString("seller_id");
                                      JSONArray pimg = product.getJSONArray("product_images");
                                      JSONObject firstimg = pimg.getJSONObject(0);
                                      cartimg = firstimg.getString("original_res");
                                      String[] img2 = cartimg.split("\\.");
                                      String imagone = productpath + sellerid + '/' + img2[0] + '(' + '2' + '0' + '0' + ')' + '.' + img2[1];
                                      int items = 2;
                                      cart.add(new CartItemoriginal(imagone, cartproductname, cartaliasname, items, cartprice, cartpid));

                                  }

                              }
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                        pincodecheck();
                          //  cartadapter.notifyDataSetChanged();
                          // stopping swipe refresh
                          // swipeRefreshLayout.setRefreshing(false);
                      } // condtion check the status 200
                      else // this is if status falied in runtime
                      {
                          Toast.makeText(CartItems.this, "Status Failed in Banner Page check ur network connection", Toast.LENGTH_LONG).show();
                      }

                  }
                  hidepDialog();

              }
          }, new Response.ErrorListener() {

              @Override
              public void onErrorResponse(VolleyError error) {}
          });
      AppController.getInstance().addToRequestQueue(jsonObjReq);
  }

  /*****************************************  second url volley ********************************************/
  public void pincodecheck() {
      if (!pincode.isEmpty()) {

          String pincon = pincode;
          int pin4 = Integer.parseInt(pincon);
          secondServiceCall(pin4, cartpid, sellerid);

      } else {
          Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
      }
  }
  public void secondServiceCall(int pin, String productid, String sellerid) {

      String pinurl = "http://192.168.0.33/sharpswebsite3/qcrest1.0/?type=pinCodeCheck&result=json&product_id=" + productid + "&seller_id=" + sellerid + "&pinCode=" + pin;
      // use this var membershipid acc to your need ...
      JsonObjectRequest jsonObjReq = new JsonObjectRequest(
          Request.Method.GET, pinurl, null,
          new Response.Listener < JSONObject > () {
              @Override
              public void onResponse(JSONObject response) {

                  if (response != null) {
                      //   int status=jsonObject.optInt("status");
                      String status = response.optString("status");
                      if (status.equalsIgnoreCase("200")) { //check the status 200 or not
                          try {
                              JSONObject responses = response.getJSONObject("response");
                              jsonarray = responses.getJSONArray(DATA);
                              if (jsonarray.length() > 0) {
                                  // looping through json and adding to movies list
                                  for (int i = 0; i < jsonarray.length(); i++) {
                                      JSONObject product = jsonarray.getJSONObject(i);
                                      process = product.getString("process");
                                      Message = product.getString("message");
                                      if (process.equalsIgnoreCase("success") && Message.equalsIgnoreCase("success")) {
                                          cartdelivery = product.getString("delivery");
                                          cartshippingcharge = product.getString("shipping_charge");
                                          String pincode = product.getString("pincode");
                                          cart.add(new CartItemoriginal(cartdelivery, cartshippingcharge, "richard feloboune"));

                                      }




                                  }
                              }
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                          cartadapter.notifyDataSetChanged();
                          // stopping swipe refresh
                          // swipeRefreshLayout.setRefreshing(false);
                      } // condtion check the status 200
                      else // this is if status falied in runtime
                      {
                          Toast.makeText(CartItems.this, "Status Failed in Banner Page check ur network connection", Toast.LENGTH_LONG).show();
                      }
                  }

                  recyleitems = (RecyclerView) findViewById(R.id.mycartitem);
                  llm = new LinearLayoutManager(CartItems.this);
                  llm.setOrientation(LinearLayoutManager.VERTICAL);
                  recyleitems.setLayoutManager(llm);
                  recyleitems.setHasFixedSize(true);
                  cartadapter = new CartlistAdapter(cart, CartItems.this);
                  Log.i(String.valueOf(cartadapter), "cartadapter");
                  recyleitems.setAdapter(cartadapter);
                  cartadapter.notifyDataSetChanged();

              }
          }, new Response.ErrorListener() {

              @Override
              public void onErrorResponse(VolleyError error) {}
          });
      AppController.getInstance().addToRequestQueue(jsonObjReq);
  }

我可以得到第一个 urloutput 而没有得到第二个。

如何解决此错误,提前致谢。

【问题讨论】:

    标签: android android-layout android-studio android-recyclerview android-volley


    【解决方案1】:

    尝试在调用第二个服务之后而不是在第一次调用之后添加这个

                 recyleitems = (RecyclerView) findViewById(R.id.mycartitem);
                 llm = new LinearLayoutManager(CartItems.this);
                 llm.setOrientation(LinearLayoutManager.VERTICAL);
                 recyleitems.setLayoutManager(llm);
                 recyleitems.setHasFixedSize(true);
                 cartadapter = new CartlistAdapter(cart, CartItems.this);
                 Log.i(String.valueOf(cartadapter), "cartadapter");
                 recyleitems.setAdapter(cartadapter);
                 cartadapter.notifyDataSetChanged();
    

    更好的方法是在onCreate 中声明您的视图,调用服务后只需在自定义ArrayList 中添加数据,在您的List 中成功添加数据后,只需调用youradapter.notifyDataSetChanged();

    【讨论】:

    • 我将适配器调用到第二个服务但出现了一些错误,然后厌倦了 oncreateview 调用了我的适配器但只显示了第一个 url 项
    • 没有得到错误但recyclerview没有显示任何东西
    猜你喜欢
    • 2018-06-05
    • 2016-09-23
    • 2020-08-09
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多