Android 中怎么利用Okhttp请求查询购物车,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了泊头免费建站欢迎大家使用!
查询购物车的model层
public class SelectCarModel { private String url="http://120.27.23.105/product/getCarts"; private HashMapmap = new HashMap<>(); public void verifySelectCarInfo(int uid, final ISelectCarPresenter iSelectCarPresenter){ map.put("uid",uid+""); OkHttpUtils.getInstance().doPost(url,map, new CallBack() { @Override public void onFailed(String msg) { iSelectCarPresenter.onFailed("请求失败"); } @Override public void onSuccess(String request) { SelectCarBean selectCarBean = GsonUtils.getInstance().fromJson(request, SelectCarBean.class); String code = selectCarBean.getCode(); if ("0".equals(code)){ List data = selectCarBean.getData(); iSelectCarPresenter.onSuccess(data); }else{ iSelectCarPresenter.onFailed("请求失败"); } } }); } }
查询购物车的接口
m层的
public interface ISelectCarPresenter { void onFailed(String msg); void onSuccess(Listdata); }
p层的
public interface ISelectCarView { void onFailed(String msg); void onSuccess(Listdata); }
查询购物车的presenter层
public class SelectCarPresenter implements ISelectCarPresenter{ private ISelectCarView iSelectCarView; private SelectCarModel selectCarModel; public SelectCarPresenter (ISelectCarView iSelectCarView){ this.iSelectCarView = iSelectCarView; selectCarModel = new SelectCarModel(); } //执行集合信息 public void excuteSelectCarData(int uid){ //传到model selectCarModel.verifySelectCarInfo(uid,this); } @Override public void onFailed(String msg) { iSelectCarView.onFailed(msg); } @Override public void onSuccess(Listdata) { iSelectCarView.onSuccess(data); } }
查询购物车的view层
public class ShoppingCartActivity extends AppCompatActivity implements ISelectCarView,View.OnClickListener{ private boolean isLogin; private SelectCarPresenter selectCarPresenter; private ExpandableListView car_elv; private Listglist = new ArrayList<>(); private List > clist = new ArrayList<>(); private CarExpandAdapter carExpandAdapter; private TextView edit_tv; private CheckBox chooseAll; private TextView totalPrice; private TextView btnAmount; private boolean isAll; private double p; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shoppingcar); //初始化数据 init(); chooseAll.setOnClickListener(this); btnAmount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ShoppingCartActivity.this, IndentActivity.class); intent.putExtra("price",p+""); startActivity(intent); } }); } /** * 查询购物车 */ private void selectShoppingCar() { selectCarPresenter = new SelectCarPresenter(this); selectCarPresenter.excuteSelectCarData(3381); carExpandAdapter = new CarExpandAdapter(this,glist,clist,this); car_elv.setAdapter(carExpandAdapter); } /** * 初始化属性 */ private void init() { car_elv = (ExpandableListView) findViewById(R.id.car_elv); edit_tv = (TextView) findViewById(R.id.edit_tv); chooseAll = (CheckBox) findViewById(R.id.chooseAll); totalPrice = (TextView) findViewById(R.id.totalPrice); btnAmount = (TextView) findViewById(R.id.btnAmount); //登录购物车 selectShoppingCar(); } @Override public void onFailed(String msg) { } @Override public void onSuccess(List
data) { glist.clear(); clist.clear(); for(int i = 0;i list = data.get(i).getList(); List cl = new ArrayList<>(); for (int j = 0; j< list.size(); j++){ cl.add(new Child(false, list.get(j).getTitle(),list.get(j).getPrice(), list.get(j).getImages(), list.get(j).getNum(),list.get(j).getSubhead(),list.get(j).getPid())); } clist.add(cl); } for(int s = 0; s < carExpandAdapter.getGroupCount(); s++){ car_elv.expandGroup(s); } carExpandAdapter.notifyDataSetChanged(); } @Override public void onResume() { super.onResume(); init(); } /** * 反选 * @param isAll */ public void getisAll(Boolean isAll) { chooseAll.setChecked(isAll); } /** * 全选按钮 * @param view */ @Override public void onClick(View view) { isAll = chooseAll.isChecked(); if (!(isAll==true)) { for (int i = 0; i < glist.size(); i++) { glist.get(i).setFlag(false); Log.e("Song","123"); } for (int i=0 ;i view层布局
二级购物车列表的适配器
public class CarExpandAdapter extends BaseExpandableListAdapter{ private Context context; private ListgroupList; private List > childList; private ShoppingCartActivity s; public static boolean flog; public CarExpandAdapter(Context context, List
groupList, List > childList,ShoppingCartActivity s) { this.context = context; this.groupList = groupList; this.childList = childList; this.s = s; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int i) { return childList.get(i).size(); } @Override public Object getGroup(int i) { return groupList.get(i); } @Override public Object getChild(int i, int i1) { return childList.get(i).get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return false; } //第一级列表viewhodel class GroupViewHolder{ CheckBox group_box; TextView group_tv; } //第二级列表的viewhodel class ChildViewHodel{ CheckBox child_box; ImageView child_iv; TextView child_tv_name; TextView child_tv_attr; TextView child_tv_price; TextView child_tv_num; TextView child_tv_minus; TextView child_tv_show_num; TextView child_tv_add; Button delete; } @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { final GroupViewHolder holder; if (view==null){ holder = new GroupViewHolder(); view = View.inflate(context, R.layout.group_layout,null); holder.group_box = view.findViewById(R.id.group_box); holder.group_tv = view.findViewById(R.id.group_tv); view.setTag(holder); }else{ holder = (GroupViewHolder) view.getTag(); } holder.group_tv.setText(groupList.get(i).getName()); holder.group_box.setChecked(groupList.get(i).isFlag()); holder.group_box.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //group布尔值变成选中状态 groupList.get(i).setFlag(holder.group_box.isChecked()); //遍历Group的集合 for (int i=0;i
适配器的一级布局
适配器二级布局
bean类Child
public class Child { /* false, list.get(j).getTitle(), (float) list.get(j).getPrice(), list.get(j).getImages(), list.get(j).getNum() */ private boolean flag; private String title; private double price; private String image; private int num; private String color; private int pid; public Child(boolean flag, String title, double price, String image, int num,String color,int pid) { this.flag = flag; this.title = title; this.price = price; this.image = image; this.num = num; this.color = color; this.pid = pid; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } }group类
public class Group { private boolean flag; private String name; public Group(boolean flag, String name) { this.flag = flag; this.name = name; } public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getName() { return name; } public void setName(String name) { this.name = name; } }看完上述内容,你们掌握Android 中怎么利用Okhttp请求查询购物车的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
分享题目:Android中怎么利用Okhttp请求查询购物车
本文地址:http://cxhlcq.com/article/jdscii.html