例:
加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
加依赖:
dependencies { compile 'com.google.code.gson:gson:2.2.4' compile 'com.squareup.okhttp3:okhttp:3.10.0' compile 'com.github.bumptech.glide:glide:3.7.0' }
MainActivity主方法:
//我想请求数据, okhttp public class MainActivity extends AppCompatActivity implements DataView ,MyAdapter.NumInterface { String dataUrl ="https://www.zhaoapi.cn/product/getProducts?pscid=1"; List<ShopingBean.DataBean> list; MyAdapter adapter; int totalPrice =0; //总价 int totalNum = 0; //总个数 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); list = (List<ShopingBean.DataBean>) msg.obj; adapter = new MyAdapter(MainActivity.this,list); lv.setAdapter(adapter); adapter.setShoping(MainActivity.this); } }; private ListView lv; private TextView tv_totalPrice; private CheckBox cb_all; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb_all = findViewById(R.id.cb); lv = findViewById(R.id.lv); //设置总价钱 tv_totalPrice = findViewById(R.id.tv_totalPrice); //lv.setAdapter(); MyDataPresenter presenter = new MyDataPresenter(this); presenter.netWork(dataUrl); //全选的一个操作 cb_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (cb_all.isChecked()){ //如果为true,让其全部选中 for (int x=0;x<list.size();x++){ list.get(x).setCheck(true); } adapter.notifyDataSetChanged(); }else{//如果不false,让其全部不选中 for (int x=0;x<list.size();x++){ list.get(x).setCheck(false); } adapter.notifyDataSetChanged(); } //总价变 changePrice(); } }); } //子线程里面 @Override public void toBackHome(List<ShopingBean.DataBean> list) { Message msg = new Message(); msg.obj= list; handler.sendMessage(msg); } @Override public void jiaNum(int position, TextView tv, Boolean isCheck) { //得到数量 int currentNum = list.get(position).getSellerid(); //数量进行加加 currentNum++; //把数量设置到bean里面去 list.get(position).setSellerid(currentNum); //刷新数据 adapter.notifyDataSetChanged(); //变总价 changePrice(); } /** * 改变总价 */ private void changePrice() { totalPrice =0; totalNum = 0; for (int x=0;x<list.size();x++){ ShopingBean.DataBean dataBean = list.get(x); if (dataBean.getCheck()){ //总价进行加加,个数进行加加 totalNum++; totalPrice+=list.get(x).getPrice()*list.get(x).getSellerid(); } } //设置总价 tv_totalPrice.setText(totalPrice+""); } @Override public void jianNum(int position,TextView tv,Boolean isCheck) { //得到数量 int currentNum = list.get(position).getSellerid(); //减到1 就不让其减 if (currentNum==1){ return; } //数量进行加加 currentNum--; //把数量设置到bean里面去 list.get(position).setSellerid(currentNum); //刷新数据 adapter.notifyDataSetChanged(); //变总价 changePrice(); } /** * checkBox的选中 */ @Override public void groupCheck(int position,boolean isCheck) { ShopingBean.DataBean dataBean = list.get(position); //设置checkBox的状态 dataBean.setCheck(isCheck); changePrice(); } }
activity_main布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:id="@+id/cb" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="全选" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="总价:"/> <TextView android:id="@+id/tv_totalPrice" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="0"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="结算"/> </LinearLayout> </LinearLayout>
DataView 线程:
public interface DataView { void toBackHome(List<ShopingBean.DataBean> list); }
ShopingBean 类:
https://www.zhaoapi.cn/product/getProducts?pscid=1
public class ShopingBean { /** * code : 0 * data : [{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":0,"pid":6,"price":7.99,"pscid":1,"salenum":234,"sellerid":22,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":2,"pid":9,"price":78.99,"pscid":1,"salenum":2356,"sellerid":2,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":5,"price":88.99,"pscid":1,"salenum":678,"sellerid":21,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":1,"price":118,"pscid":1,"salenum":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":7,"price":120.01,"pscid":1,"salenum":1266,"sellerid":23,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":23,"price":188,"pscid":1,"salenum":233,"sellerid":16,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":0,"pid":3,"price":198,"pscid":1,"salenum":1234,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":16,"price":199,"pscid":1,"salenum":777,"sellerid":9,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":2,"pid":15,"price":233.99,"pscid":1,"salenum":666,"sellerid":8,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:43:53","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":0,"pid":12,"price":256,"pscid":1,"salenum":0,"sellerid":5,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}] * msg : 请求成功 * page : 0 */ private String code; private String msg; private String page; private List<DataBean> data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * bargainPrice : 111.99 * createtime : 2017-10-03T23:53:28 * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg * itemtype : 0 * pid : 6 * price : 7.99 * pscid : 1 * salenum : 234 * sellerid : 22 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; boolean isCheck; public void setCheck(boolean isCheck){ this.isCheck = isCheck; } public boolean getCheck(){ return isCheck; } public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } }
MyAdapter 适配器类:
public class MyAdapter extends BaseAdapter { Context context; List<ShopingBean.DataBean> list; NumInterface numInterface; public MyAdapter(Context context, List<ShopingBean.DataBean> list) { this.context = context; this.list = list; } /** * 目的是把NumInterface传过来, * @param numInterface */ public void setShoping(NumInterface numInterface){ this.numInterface = numInterface; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView==null){ convertView = View.inflate(context,R.layout.lv_item,null); holder = new ViewHolder(convertView); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } String imageUrl ="https://img-my.csdn.net/uploads/201407/26/1406383264_4787.jpg"; //设置值的操作 holder.lv_title.setText(list.get(position).getTitle()); holder.lv_price.setText(list.get(position).getPrice()+""); //数量 Sellerid数量 holder.lv_num.setText(list.get(position).getSellerid()+""); //刷新适配器时会回调getView方法 holder.lv_cb.setChecked(list.get(position).getCheck()); Glide.with(context).load(imageUrl).into(holder.lv_iv); //得到dataBean ShopingBean.DataBean dataBean = list.get(position); if (dataBean.getCheck()){ dataBean.setCheck(true); }else { dataBean.setCheck(false); } //checkBox checkBox,,的监听事件 holder.lv_cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.get(position).setCheck(holder.lv_cb.isChecked()); //一个是position. 一个是状态 numInterface.groupCheck(position,holder.lv_cb.isChecked()); } }); //按扭加的操作 holder.lv_jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { numInterface.jiaNum(position,holder.lv_num,holder.lv_cb.isChecked()); } }); //数量减的按钮 holder.lv_jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { numInterface.jianNum(position,holder.lv_num,holder.lv_cb.isChecked()); } }); return convertView; } class ViewHolder{ CheckBox lv_cb; ImageView lv_iv; TextView lv_title,lv_num,lv_price; Button lv_jian,lv_jia; ViewHolder(View conerView){ lv_cb = conerView.findViewById(R.id.lv_cb); lv_iv = conerView.findViewById(R.id.lv_iv); lv_title = conerView.findViewById(R.id.lv_title); lv_price = conerView.findViewById(R.id.lv_price); lv_jian = conerView.findViewById(R.id.lv_jian); lv_jia = conerView.findViewById(R.id.lv_jia); lv_num = conerView.findViewById(R.id.lv_num); } } //修改数量的接口 public interface NumInterface{ void jiaNum(int position,TextView tv,Boolean isCheck); void jianNum(int position,TextView tv,Boolean isCheck); void groupCheck(int position,boolean isCheck); } }
lv_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/lv_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:layout_toRightOf="@id/lv_cb" android:id="@+id/lv_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_alignBottom="@id/lv_iv" android:layout_toRightOf="@id/lv_iv" android:layout_width="200dp" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/lv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/lv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/lv_jian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-"/> <TextView android:id="@+id/lv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-"/> <Button android:id="@+id/lv_jia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+"/> </LinearLayout> </LinearLayout> </RelativeLayout>
MyDataPresenter 类:
public class MyDataPresenter implements DataPresenter{ private final MyDataMoudle myDataMoudle; DataView dataView; public MyDataPresenter(DataView dataView){ this.dataView = dataView; myDataMoudle = new MyDataMoudle(); } //M层得到数据,传给p层 ,传给view 层 @Override public void success(List<ShopingBean.DataBean> list) { dataView.toBackHome(list); } @Override public void error() { } //请求数据的方法 public void netWork(String dataUrl) { myDataMoudle.getData(dataUrl,this); } }
MyDataMoudle 类:
public class MyDataMoudle implements DataMoudle { //请求数据的 @Override public void getData(String url, final DataPresenter presenter) { //请求数据 OkhtttpUtils.doGet(url, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { //可以认为是请求成功 String json = response.body().string(); Gson gson = new Gson(); ShopingBean shopingBean = gson.fromJson(json, ShopingBean.class); //得到数据,服务器的 List<ShopingBean.DataBean> list = shopingBean.getData(); //回调给p层 presenter.success(list); } }); } }
OkhtttpUtils
public class OkhtttpUtils { //单例模式,把构造方法进行私有化 private OkhtttpUtils(){}; static OkHttpClient client; public static OkHttpClient getInstance(){ if (client==null) { //更加安全 synchronized (OkhtttpUtils.class) { //缓存的地方 mnt/sdcard File file = new File(Environment.getExternalStorageDirectory(), "cache11"); client = new OkHttpClient().newBuilder() .readTimeout(3000, TimeUnit.SECONDS) //设置读取超时时间 .connectTimeout(3000, TimeUnit.SECONDS) //设置连接的超时时间 .cache(new Cache(file, 10 * 1024)) .build(); } } return client; } /** * get请求 * Callback 是一个接口 */ public static void doGet(String url, Callback callback){ //1:拿到okhttpclient 对像 OkHttpClient client = getInstance(); //2:进行请求的操作 Request request = new Request.Builder() .url(url) .build(); client.newCall(request).enqueue(callback); } //post请求 /** * @param url 请求的地址 * @param parms 请求的参数 * @param callback callback */ public static void doPost(String url, Map<String,String> parms, Callback callback){ //得到客户端的对像 OkHttpClient client = getInstance(); //不是FormBody,而是一个Builder FormBody.Builder body = new FormBody.Builder(); //key value for (String key:parms.keySet()){ //value的值 body.add(key,parms.get(key)); } Request request = new Request.Builder() .url(url) .post(body.build()) .build(); client.newCall(request).enqueue(callback); } //用来上传图片的 //url , 图片 ,参数 Callback public static void upImage(String url,File file,String filenName,Map<String,String> params,Callback callback){ OkHttpClient client = getInstance(); //requestBody的实现类 Formbody MultipartBody.Builder builder = new MultipartBody.Builder(); if (params!=null){ for (String key :params.keySet()){ builder.addFormDataPart(key,params.get(key)); } } //设置类型 builder.setType(MultipartBody.FORM); builder.addFormDataPart("file",filenName, RequestBody.create(MediaType.parse("application/octet-stream"),file)); // builder.setType(MultipartBody.FORM); // builder.addFormDataPart("file",filenName,RequestBody.create(MediaType.parse("application/octet-stream"),file)); //builder.addFormDataPart("file","aa.png",builder.build()); Request request = new Request.Builder() .url(url) .post(builder.build()) .build(); client.newCall(request).enqueue(callback); } }
DataMoudle 接口:
public interface DataMoudle { void getData(String url, DataPresenter presenter); }
DataPresenter 接口:
public interface DataPresenter { void success(List<ShopingBean.DataBean> list); void error(); }