MVP+XRecyclerView+okhttp+glide,列表或者网格显示所加载的数据

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile files('libs/glide-3.7.0.jar')
}

glide-3.7.0.jar      gson-2.3.1.jar          okio-1.13.0.jar

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.com.zk2">

    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


-------------Layout页面布局---------------------------

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <RelativeLayout
        android:id="@+id/head"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_toLeftOf="@+id/bt_change"
            android:gravity="center_horizontal"
            android:text="搜索商品"
            android:textColor="@android:color/black"
            android:textSize="30sp" />

        <Button
            android:id="@+id/bt_change"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/grid_icon" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/head">

        <EditText
            android:id="@+id/et_goods"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/bt_search"
            android:hint="请输入要查询的商品" />

        <Button
            android:id="@+id/bt_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="搜索" />

    </RelativeLayout>

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/xrv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/body"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>



gri_layout.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">

    <ImageView
        android:id="@+id/item_gri_iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/body"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:id="@+id/body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_gri_tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="asdasda" />

        <TextView
            android:id="@+id/item_gri_tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="asdasda" />

    </LinearLayout>


</RelativeLayout>




lin_layout.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="600dp">

    <ImageView
        android:id="@+id/item_li_iv"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_toRightOf="@+id/item_li_iv"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_li_tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="asdsa" />

        <TextView
            android:id="@+id/item_li_tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="asdsa" />
    </LinearLayout>
</RelativeLayout>

-------------.JAVA------------------------------------




activity包         MainActivity.java

 
package app.com.zk2.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import app.com.zk2.R;
import app.com.zk2.adapter.MyAdapter;
import app.com.zk2.bean.NetDataBean;
import app.com.zk2.presenter.MySearchPresenter;
import app.com.zk2.view.ISearchView;

public class MainActivity extends AppCompatActivity implements ISearchView, View.OnClickListener {
    //实现presenter和其他的一些变量
    MySearchPresenter presenter = new MySearchPresenter(this, this);
    private Button mChangeBt;
    private RelativeLayout mHead;
    private EditText mGoodsEt;
    private Button mSearchBt;
    private RelativeLayout mBody;
    private XRecyclerView mXrv;
    private int num = 1;
    private int aa = 1;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //寻找控件
        initView();
    }

    private void initView() {
        mChangeBt = (Button) findViewById(R.id.bt_change);
        mChangeBt.setOnClickListener(this);
        mHead = (RelativeLayout) findViewById(R.id.head);
        mGoodsEt = (EditText) findViewById(R.id.et_goods);
        mSearchBt = (Button) findViewById(R.id.bt_search);
        mSearchBt.setOnClickListener(this);
        mBody = (RelativeLayout) findViewById(R.id.body);
        mXrv = (XRecyclerView) findViewById(R.id.xrv);
    }

    @Override
    public void ShowView(NetDataBean bean) {
        //view 层 的方法 用来更新ui
        Toast.makeText(MainActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        mXrv.setLayoutManager(manager);
        final MyAdapter adapter = new MyAdapter(MainActivity.this, bean);
        mXrv.setAdapter(adapter);
        //XRecyclerview的上拉下拉方法
        mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //刷新
                        presenter.getdata(mGoodsEt.getText().toString(), "1");
                        adapter.notifyDataSetChanged();
                        mXrv.refreshComplete();
                    }
                }, 900);
            }

            @Override
            public void onLoadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //加载
                        num++;
                        presenter.getdata(mGoodsEt.getText().toString(), num + "");
                        adapter.notifyDataSetChanged();
                        mXrv.loadMoreComplete();

                    }
                }, 900);
            }
        });

    }

    @Override
    public void onClick(View v) {
        //点击事件
        switch (v.getId()) {
            case R.id.bt_change:
                //根据aa变量是奇数还是偶数来判断加载哪种布局返回哪张图片
                aa++;
                if (aa % 2 == 0) {
                    //   MainActivity.this, 2   2是两列形式布局
                    GridLayoutManager manager = new GridLayoutManager(MainActivity.this, 2);
                    mXrv.setLayoutManager(manager);
                    //给按钮赋一个图片背景
                    mChangeBt.setBackgroundResource(R.drawable.grid_icon);
                }
                if (aa % 2 == 1) {
                    LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
                    mXrv.setLayoutManager(manager);
                    mChangeBt.setBackgroundResource(R.drawable.lv_icon);
                }
                break;
            case R.id.bt_search:
                //点击搜索按钮时触发presenter的获取数据方法
                presenter.getdata(mGoodsEt.getText().toString(), "1");
                break;
            default:
                break;
        }
    }

    //实现presenter内部的防止内存溢出方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destory();
    }
}


adapter包             MyAdapter.java

package app.com.zk2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

import app.com.zk2.R;
import app.com.zk2.bean.NetDataBean;

/**
 *适配器
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myholder> {
    //两个参数  上下文,和数据源
    Context context;
    NetDataBean bean;
    public MyAdapter(Context context, NetDataBean bean) {
        this.context = context;
        this.bean = bean;
    }

    @Override
    public myholder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局返回到holder

        View view=View.inflate(context,R.layout.lin_layout,null);
        myholder myholder=new myholder(view);

        return myholder;
    }

    @Override
    public void onBindViewHolder(myholder holder, int position) {
        //通过数据源内的图片字符串 通过split截取成数组
         String[] imgdata=bean.getData().get(position).getImages().split("\\|");
         Glide.with(context)
                 .load(imgdata[0])
                 .into(holder.item_li_iv);
        //设置控件 属性
         holder.item_li_tv1.setText(bean.getData().get(position).getTitle());
         holder.item_li_tv2.setText(bean.getData().get(position).getPrice()+"¥");
    }
    @Override
    public int getItemCount() {
        return bean.getData()==null?0:bean.getData().size();
    }
    class myholder extends RecyclerView.ViewHolder{
        ImageView item_li_iv;
//        ImageView item_gri_iv;
        TextView item_li_tv1;
        TextView item_li_tv2;
//        TextView item_gri_tv1;
//        TextView item_gri_tv2;
        public myholder(View itemView) {
            super(itemView);
            item_li_iv=itemView.findViewById(R.id.item_li_iv);
//            item_gri_iv=itemView.findViewById(R.id.item_gri_iv);
            item_li_tv1=itemView.findViewById(R.id.item_li_tv1);
            item_li_tv2=itemView.findViewById(R.id.item_li_tv2);
//            item_gri_tv1=itemView.findViewById(R.id.item_gri_tv1);
//            item_gri_tv2=itemView.findViewById(R.id.item_gri_tv2);
        }
    }

}



bean包         NetDataBean.java

package app.com.zk2.bean;

import java.util.List;

/**
 * bean类
 */

public class NetDataBean {

    /**
     * msg : 查询成功
     * code : 0
     * data : [{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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":1,"pid":80,"price":777,"pscid":40,"salenum":776,"sellerid":1,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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":79,"price":888,"pscid":40,"salenum":5454,"sellerid":23,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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":1,"pid":78,"price":999,"pscid":40,"salenum":656,"sellerid":22,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","itemtype":0,"pid":57,"price":5199,"pscid":40,"salenum":4343,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":5599,"createtime":"2017-10-10T17:30:32","detailUrl":"https://item.m.jd.com/product/4824715.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n12/jfs/t7768/184/1153704394/148460/f42e1432/599a930fN8a85626b.jpg!q70.jpg","itemtype":0,"pid":59,"price":5599,"pscid":40,"salenum":675,"sellerid":3,"subhead":"游戏本选择4G独显,拒绝掉帧】升级版IPS全高清防眩光显示屏,WASD方向键颜色加持,三大出风口立体散热!","title":"戴尔DELL灵越游匣15PR-6648B GTX1050 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 128GSSD+1T 4G独显 IPS)黑"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","itemtype":1,"pid":58,"price":6399,"pscid":40,"salenum":545,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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":1,"pid":63,"price":10000,"pscid":40,"salenum":3232,"sellerid":7,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-03T23:43:53","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":64,"price":11000,"pscid":40,"salenum":0,"sellerid":8,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","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":2,"pid":65,"price":12000,"pscid":40,"salenum":868,"sellerid":9,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","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":1,"pid":66,"price":13000,"pscid":40,"salenum":7676,"sellerid":10,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}]
     * page : 1
     */

    private String msg;
    private String code;
    private String page;
    private List<DataBean> data;

    public String getMsg() {
        return msg;
    }

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

    public String getCode() {
        return code;
    }

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

    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 : 11800.0
         * createtime : 2017-10-14T21:38:26
         * 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 : 1
         * pid : 80
         * price : 777.0
         * pscid : 40
         * salenum : 776
         * sellerid : 1
         * 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包      ISearchModel.java

package app.com.zk2.model;

import okhttp3.Callback;

/**
 * Created by asus on 2017/11/13.
 */

//model接口
public interface ISearchModel {
    public void GetNetData(String keywords, String page, Callback callback);
}

model包 MySearchModel.java

package app.com.zk2.model;


import app.com.zk2.util.OkHttp3Utils;
import okhttp3.Callback;

/**
 * Model实现类
 */

public class MySearchModel implements ISearchModel {
    //传三个参数 一个是分页加载时用到的 page 还有  输入框内搜索的值,还有callback 用来在presenter内拿出bean
    @Override
    public void GetNetData(String keywords, String page, Callback callback) {
        OkHttp3Utils.doGet("http://120.27.23.105/product/searchProducts?keywords=" + keywords + "&page=" + page + "&source=android", callback);
    }
}



presenter包        MySearchPresenter.java

package app.com.zk2.presenter;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;

import app.com.zk2.bean.NetDataBean;
import app.com.zk2.model.MySearchModel;
import app.com.zk2.util.OnUiCallback;
import app.com.zk2.view.ISearchView;
import okhttp3.Call;

/**
 * Created by asus on 2017/11/13.
 */

public class MySearchPresenter {
    Context context;
    ISearchView view;
    MySearchModel model;

    //将 mvp三层关联
    public MySearchPresenter(Context context, ISearchView view) {
        this.context = context;
        this.view = view;
        model = new MySearchModel();
    }

    public void getdata(String keyword, String page) {
        model.GetNetData(keyword, page, new OnUiCallback() {
            @Override
            public void onFailed(Call call, IOException e) {
                //失败方法
            }

            @Override
            public void onSuccess(String result) {
                //获取数据成功时将返回的json 变成bean
                Log.i("3333333", "onSuccess: " + result.toString());
                Gson gson = new Gson();
                NetDataBean bean = gson.fromJson(result, NetDataBean.class);
                view.ShowView(bean);

            }
        });
    }

    //用来防止内存溢出
    public void destory() {
        this.view = null;
    }


}





util包         OkHttp3Utils.java

package app.com.zk2.util;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 1. 类的用途
 * 2. @author 
 * 
 */

public class OkHttp3Utils {

    private static OkHttpClient okHttpClient = null;

    public OkHttp3Utils() {
    }

    private static OkHttpClient getOkHttpClient() {
        synchronized (OkHttp3Utils.class) {
            if (okHttpClient == null) {
                okHttpClient = new OkHttpClient();
            }
        }
        return okHttpClient;
    }

    //上传文件
    public static void loadFile(String url, File file,String fileName){
        OkHttpClient okHttpClient = getOkHttpClient();
        //设置文件类型
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"),file);
        //设置请求体
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image",fileName,requestBody)
                .build();
        //请求方式
        Request request = new Request.Builder().url(url).post(body).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 {
                Log.i("成功","成功");
            }
        });
    }

    /**
     * 1.接口地址
     * 2.接口回调
     */
    public static void doGet(String url,Callback callback){
        OkHttpClient okHttpClient = getOkHttpClient();
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * 1.地址
     * 2.接口回调
     * 3.请求体
     */

    public static void doPost(String url, Map<String,String> map,Callback callback){
        OkHttpClient okHttpClient = getOkHttpClient();
        FormBody.Builder builder = new FormBody.Builder();
        //遍历map集合   设置请求体
        for (String mapKey : map.keySet()){
           builder.add(mapKey,map.get(mapKey));
        }
        //设置请求方式
        Request request = new Request.Builder().url(url).post(builder.build()).build();
        //执行请求方式    接口回调
        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     *1.下载地址
     */
    public static void doDown(String url,Callback callback){
        OkHttpClient okHttpClient = getOkHttpClient();
        Request build = new Request.Builder().url(url).build();

        okHttpClient.newCall(build).enqueue(callback);
    }
}



util包      OkHttpUtils.java

package app.com.zk2.util;

import android.os.Handler;

import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by lenovo on 2017/10/12.
 */
public class OkHttpUtils {
    private Handler handler=new Handler();
    public Handler getHandler(){
        return handler;
    }
    //单例
    private static OkHttpUtils okHttpUtils=new OkHttpUtils();
    private OkHttpUtils(){};
    public static OkHttpUtils getInstance(){
        return okHttpUtils;
    }

    private OkHttpClient client;
    private void initOkHttpClient(){
        if (client==null){
            client=new OkHttpClient.Builder().build();
        }
    }
    //公用的get请求方法  完成的功能不确定
    public void doGet(String url, Callback callback){
        initOkHttpClient();
        Request request=new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }
}



util包 OnUiCallback.java

package app.com.zk2.util;

import android.os.Handler;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by lenovo on 2017/10/12.
 */
public abstract class OnUiCallback implements Callback {
    private Handler handler = OkHttpUtils.getInstance().getHandler();

    public abstract void onFailed(Call call, IOException e);

    public abstract void onSuccess(String result);


    @Override
    public void onFailure(final Call call, final IOException e) {
        //该方法就是把  线程post到handler所在的线程
        handler.post(new Runnable() {
            @Override
            public void run() {
//                e.getMessage()
                onFailed(call, e);
            }
        });
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        final String result = response.body().string();
        handler.post(new Runnable() {
            @Override
            public void run() {
                onSuccess(result);
            }
        });
    }
}




view包       ISearchView.java

package app.com.zk2.view;


import app.com.zk2.bean.NetDataBean;

/**
 * Created by asus on 2017/11/13.
 */

public interface ISearchView {
    public void ShowView(NetDataBean bean);
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值