1.接口,契约类
public class ContentInferface {
//m层
public interface MyModelInterface{
public void toRequest(String url, Map<String,String>map, int type, MyModel.MyCallBack myCallBack);
}
//p层
public interface PerpsoneInterface{
public void toModel();
public void onDestroy();
}
//v层
public interface VInterface{
public void vDisply(List<JsonBean.ResultBean.RxxpBean.CommodityListBean> mList);
}
}
2.OkHttp
public class OkHttp {
public OkHttpClient okHttpClient;
public static OkHttp okHttp;
//构造方法私有化
private OkHttp(){
okHttpClient = new OkHttpClient();
}
//单例模式
public static synchronized OkHttp getInOkHttp(){
if (okHttp == null){
okHttp = new OkHttp();
}
return okHttp;
}
//Get请求
public void doGet(String url, final Map<String,String>map, final Handler mH, final int type){
if (map!=null && map.size()>0){
//遍历集合
String str = "";
StringBuilder builder = new StringBuilder();
//拼接参数
for (String key:map.keySet()){
String pkey = key;
String s = map.get(pkey);
builder.append("=")
.append(s)
.append("&");
}
str = builder.toString();
//因为for循环要遍历键值队,所以会多拼接一个&
int i = str.lastIndexOf("&");
str =str.substring(0,i);
url = url+"?"+str;
}
//发送请求
Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
//使用okhttp异步请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//请求成功,获取json字符串
String string = response.body().string();
Message message = new Message();
message.obj = string;
message.arg1 = type;
mH.sendMessage(message);
}
});
}
}
3.V层MyActity
public class MyActivity extends Activity implements ContentInferface.VInterface {
public List<JsonBean.ResultBean.RxxpBean.CommodityListBean> list = new ArrayList<JsonBean.ResultBean.RxxpBean.CommodityListBean>();
public RecyclerView rec_list;
public MyAdpter myAdpter;
public ContentInferface.PerpsoneInterface perpsoneInterface;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
rec_list = findViewById(R.id.rec_list);
//实例化布局管理器
LinearLayoutManager manager = new LinearLayoutManager(this);
//设置管理显示方式
manager.setOrientation(LinearLayoutManager.VERTICAL);
//衔接
rec_list.setLayoutManager(manager);
//适配器
myAdpter = new MyAdpter(list, MyActivity.this);
rec_list.setAdapter(myAdpter);
//p层接口对象实例化,new的是p层的实现类
perpsoneInterface = new MyPerpsone(this);
//请求数据
perpsoneInterface.toModel();
}
@Override
public void vDisply(List<JsonBean.ResultBean.RxxpBean.CommodityListBean> mList) {
//添加到集合中
list.addAll(mList);
myAdpter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
perpsoneInterface.onDestroy();
perpsoneInterface = null;
}
}
4.M层
public class MyModel implements ContentInferface.MyModelInterface {
//全局变量
MyCallBack myCallBack;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int type = msg.arg1;
switch (type){
case 1:
String json = (String) msg.obj;
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(json, JsonBean.class);
myCallBack.succer(jsonBean);
break;
}
}
};
@Override
public void toRequest(String url, Map<String, String> map, int type, MyCallBack myCallBack) {
this.myCallBack = myCallBack;
OkHttp.getInOkHttp().doGet(url,map,handler,type);
}
//接口
public interface MyCallBack{
public void succer(Object obj);
public void error(String str);
}
}
5.P层
public class MyPerpsone implements ContentInferface.PerpsoneInterface{
//获取m接口对象
ContentInferface.MyModelInterface myModelInterface;
//获取v层接口对象
ContentInferface.VInterface vInterface;
//构造方法
public MyPerpsone(ContentInferface.VInterface vInterface) {
myModelInterface = new MyModel();
this.vInterface = vInterface;
}
@Override
public void toModel() {
myModelInterface.toRequest("http://172.17.8.100/small/commodity/v1/commodityList", null, 1, new MyModel.MyCallBack() {
@Override
public void succer(Object obj) {
//拿到返回对象list
//通过接口把list对象传到v层
JsonBean jsonBean = (JsonBean) obj;
vInterface.vDisply(jsonBean.getResult().getRxxp().getCommodityList());
}
@Override
public void error(String str) {
}
});
}
@Override
public void onDestroy() {
vInterface = null;
}
}
6.适配器
public class MyAdpter extends RecyclerView.Adapter<MyAdpter.holder>{
public List<JsonBean.ResultBean.RxxpBean.CommodityListBean> mList;
public Context mContext;
public MyAdpter(List<JsonBean.ResultBean.RxxpBean.CommodityListBean> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@NonNull
@Override
public MyAdpter.holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
return new holder(view);
}
//为控件设置值
@Override
public void onBindViewHolder(@NonNull MyAdpter.holder holder, int i) {
String name1 = mList.get(i).getCommodityName();
String pic = mList.get(i).getMasterPic();
holder.name.setText(name1);
Glide.with(mContext).load(pic).into(holder.image);
}
@Override
public int getItemCount() {
if (mList!=null){
return mList.size();
}
return 0;
}
public class holder extends RecyclerView.ViewHolder{
public ImageView image;
public TextView name;
public holder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
name = itemView.findViewById(R.id.name);
}
}
}