OkHttp

package com.bw.zsd.myapplication0215.util;
import android.os.Handler;
import android.os.Message;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
 * @Auther: ZSD
 * @Date: 2019/2/16 0016 21:47:52
 * @Description:
 */
public class OkhttpClientUtile {
    static String info = "";
    public static void PostRequest(int rt, String phone, String pwd, final Handler handler) {
        String url = "";
        if (rt == 0) {
            //登录接口
            url = "http://172.17.8.100/small/user/v1/login";
        } else {
            url = "http://172.17.8.100/small/user/v1/register";
        }
        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody builder = new FormBody.Builder()
                .add("phone", phone)
                .add("pwd", pwd)
                .build();
        final Request request = new Request.Builder()
                .post(builder)
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                info = "您注册失败了!";
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonStr = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(jsonStr);
                    info = jsonObject.getString("message");
                    Message message = new Message();
                    message.obj = info;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void getRequest(final Handler mhandler) {
        OkHttpClient okHttpClient = new OkHttpClient();
         Request request = new Request.Builder()
                .get()
                .url("http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=3&page=1&count=5")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Message message = new Message();
                message.obj=string;
                mhandler.sendMessage(message);
            }
        });
    }
}

Adapter

package com.bw.zsd.myapplication0215.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bw.zsd.myapplication0215.R;
import com.bw.zsd.myapplication0215.bean.CartBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: ZSD
 * @Date: 2019/2/17 0017 16:08:18
 * @Description:
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private List<CartBean.ResultBean> list;
    private Context context;
    public RecyclerViewAdapter( List<CartBean.ResultBean> list,Context context) {
        this.context = context;
        this.list = list;
    }
    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.reitem, null,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder viewHolder, int i) {
        String masterPic = list.get(i).getMasterPic();
        String commodityName = list.get(i).getCommodityName();
        Glide.with(context).load(masterPic).placeholder(R.drawable.ic_launcher_background).error(R.drawable.ic_launcher_background).into(viewHolder.imageView);
        viewHolder.title.setText(commodityName);
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }else {
         return 0;
        }
    }

    public class ViewHolder extends  RecyclerView.ViewHolder{
        public ImageView masterPic;
        private  TextView title,info;
        private  TextView pic_id;
        private final ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title_id);
            pic_id = itemView.findViewById(R.id.pic_id);
            info = itemView.findViewById(R.id.infmention_id);
            imageView = itemView.findViewById(R.id.image_id);
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值