购物车之详情页面跳转至购物车


首先依赖导入:

compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.code.gson:gson:2.8.2'
compile 'io.github.openfeign:feign-gson:9.5.1'
compile 'com.android.support:recyclerview-v7:24.0.0-alpha1'
compile 'com.github.bumptech.glide:glide:3.7.0'

之后MVP框架:

net包

Api接口类:

public static String DETAIL = "https://www.zhaoapi.cn/product/getProductDetail";   //详情接口
public static String ADDCART = "https://www.zhaoapi.cn/product/getCarts";           //添加购物车
public static String GETCART = "https://www.zhaoapi.cn/product/addCart";            //获得展示购物车(二级列表 需传uid,pid)

HttpUtils类:

package com.bwie.detailsdemo.net;

import java.util.Map;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by admin on 2017/12/18.
 */

public class HttpUtils {

    private static volatile HttpUtils httpUtils;
    private final OkHttpClient client;

    private HttpUtils(){
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor())
                .build();
    }

    public static HttpUtils getHttpUtils(){
        if (httpUtils == null){
            synchronized (HttpUtils.class){
                if (httpUtils == null){
                    httpUtils = new HttpUtils();
                }
            }
        }
        return httpUtils;
    }

    public void doGet(String url, Callback callback){
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }

    public void doPost(String url, Map<String, String> params, Callback callback){
        if (params == null) {
            throw new RuntimeException("参数为空了");
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder().url(url).post(formBody).build();
        client.newCall(request).enqueue(callback);
    }
}

自定义拦截器MyInterceptor类:

package com.bwie.detailsdemo.net;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by admin on 2017/12/18.
 */

public class MyInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (request.method().equals("GET")){
            String url = request.url().url().toString();
            url += "&source=android";
            Request newRequest = request.newBuilder().url(url).build();
            return chain.proceed(newRequest);
        }else {
            FormBody formBody = (FormBody) request.body();
            FormBody.Builder builder = new FormBody.Builder();
            for (int i = 0; i <formBody.size(); i++){
                builder.add(formBody.name(i), formBody.value(i));
            }
            builder.add("source", "android");
            FormBody newFormBody = builder.build();
            Request newRequest = request.newBuilder().url(request.url().url().toString()).post(newFormBody).build();
            return chain.proceed(newRequest);
        }
    }
}

接口回调类OnNetListener:

package com.bwie.detailsdemo.net;

/**
 * Created by admin on 2017/12/18.
 */

public interface OnNetListener<T> {

    public void onSuccess(T t);

    public void onFailure(Exception e);
}

模块展示代码:

详情页面MVP:

Bean包

DetailBean


package com.example.peng.goodscard_1510d.bean;


/**
 * Created by peng on 2017/12/14.
 */


public class DetailsBean {


    /**
     * msg :
     * seller : {"description":"我是商家15","icon":"http://120.27.23.105/images/icon.png","name":"商家15","productNums":999,"score":5,"sellerid":15}
     * code : 0
     * data : {"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","itemtype":0,"pid":71,"price":32999,"pscid":40,"salenum":4242,"sellerid":15,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}
     */


    private String msg;
    private SellerBean seller;
    private String code;
    private DataBean data;


    public String getMsg() {
        return msg;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }


    public SellerBean getSeller() {
        return seller;
    }


    public void setSeller(SellerBean seller) {
        this.seller = seller;
    }


    public String getCode() {
        return code;
    }


    public void setCode(String code) {
        this.code = code;
    }


    public DataBean getData() {
        return data;
    }


    public void setData(DataBean data) {
        this.data = data;
    }


    public static class SellerBean {
        /**
         * description : 我是商家15
         * icon : http://120.27.23.105/images/icon.png
         * name : 商家15
         * productNums : 999
         * score : 5.0
         * sellerid : 15
         */


        private String description;
        private String icon;
        private String name;
        private int productNums;
        private double score;
        private int sellerid;


        public String getDescription() {
            return description;
        }


        public void setDescription(String description) {
            this.description = description;
        }


        public String getIcon() {
            return icon;
        }


        public void setIcon(String icon) {
            this.icon = icon;
        }


        public String getName() {
            return name;
        }


        public void setName(String name) {
            this.name = name;
        }


        public int getProductNums() {
            return productNums;
        }


        public void setProductNums(int productNums) {
            this.productNums = productNums;
        }


        public double getScore() {
            return score;
        }


        public void setScore(double score) {
            this.score = score;
        }


        public int getSellerid() {
            return sellerid;
        }


        public void setSellerid(int sellerid) {
            this.sellerid = sellerid;
        }
    }


    public static class DataBean {
        /**
         * bargainPrice : 11800.0
         * createtime : 2017-10-03T23:53:28
         * detailUrl : https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1
         * images : https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg
         * itemtype : 0
         * pid : 71
         * price : 32999.0
         * pscid : 40
         * salenum : 4242
         * sellerid : 15
         * subhead : 购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)
         * title : 全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G
         */


        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;


        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;
        }
    }
}

Model层

DetailsService接口类

package com.example.peng.goodscard_1510d.model;


import com.example.peng.goodscard_1510d.bean.DetailsBean;
import com.example.peng.goodscard_1510d.net.OnNetListener;


import java.util.Map;


/**
 * Created by peng on 2017/12/14.
 */


public interface DetailsService {
    void getProductDetail(Map<String, String> params, OnNetListener<DetailsBean> onNetListener);
}


<

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值