效果图
权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="samsung.com.searchproduct"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Main2Activity"></activity> </application> </manifest>依赖 gradle
apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { applicationId "samsung.com.searchproduct" minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } 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.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.jwenfeng.pulltorefresh:library:1.0.3' compile files('libs/gson-2.8.0.jar') android { defaultConfig { multiDexEnabled true } } }布局activity_main
<?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" tools:context="samsung.com.searchproduct.MainActivity"> <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="40dp" android:text="搜索商品" android:textSize="25sp" android:gravity="center" /> <ImageView android:id="@+id/right" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/grid_icon" android:layout_alignParentRight="true" /> <RelativeLayout android:id="@+id/rel" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@+id/tv_show" android:layout_alignParentStart="true"> <EditText android:id="@+id/et_sousuo" android:layout_width="300dp" android:layout_height="50dp" android:drawableLeft="@drawable/fdj" android:hint="请输入关键字" /> <Button android:id="@+id/btn_sousuo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/et_sousuo" android:text="搜索" /> </RelativeLayout> <com.jwenfeng.library.pulltorefresh.PullToRefreshLayout android:layout_width="match_parent" android:id="@+id/mpull" android:layout_below="@+id/rel" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/re_view" android:layout_width="match_parent" android:layout_height="wrap_content" ></android.support.v7.widget.RecyclerView> </com.jwenfeng.library.pulltorefresh.PullToRefreshLayout> </RelativeLayout >布局activity_main2
<?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" tools:context="samsung.com.searchproduct.Main2Activity"> <WebView android:id="@+id/web" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </RelativeLayout >布局item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/item_img" android:layout_width="100dp" android:layout_height="100dp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_toRightOf="@+id/item_img"> <TextView android:id="@+id/item_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:text="title" android:textSize="16sp" /> <TextView android:id="@+id/item_bargainPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/item_title" android:text="售价" android:layout_marginTop="10dp" android:textColor="#ccc" android:textSize="14sp" /> <TextView android:id="@+id/item_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/item_title" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/item_bargainPrice" android:text="售价" android:layout_marginTop="10dp" android:textColor="#f00" android:textSize="14sp" /> </RelativeLayout> </LinearLayout>主包下MainActivity类
package samsung.com.searchproduct; import android.content.Intent; 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.support.v7.widget.RecyclerView; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import com.jwenfeng.library.pulltorefresh.BaseRefreshListener; import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout; import java.util.ArrayList; import java.util.List; import samsung.com.searchproduct.adapter.NewsListAdapter; import samsung.com.searchproduct.bean.JavaBean; import samsung.com.searchproduct.httputils.INewsView; import samsung.com.searchproduct.presenter.NewsPresenter; public class MainActivity extends AppCompatActivity implements INewsView { private RecyclerView re_view; private List<JavaBean.DataBean> list; private NewsListAdapter adapter; private Button btn_sousuo; private EditText et_sousuo; private NewsPresenter presenter; private ImageView right; private boolean flag = true; private PullToRefreshLayout mpull; private int page = 1; private String show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); re_view = (RecyclerView) findViewById(R.id.re_view); btn_sousuo = (Button) findViewById(R.id.btn_sousuo); et_sousuo = (EditText) findViewById(R.id.et_sousuo); right = (ImageView) findViewById(R.id.right); mpull = (PullToRefreshLayout) findViewById(R.id.mpull); list = new ArrayList<>(); adapter = new NewsListAdapter(this, list,1); LinearLayoutManager manager = new LinearLayoutManager(this); re_view.setLayoutManager(manager); re_view.setAdapter(adapter); presenter = new NewsPresenter(); presenter.attachView(this); btn_sousuo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { show = et_sousuo.getText().toString().trim(); presenter.getNews(show,page); } }); right.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (flag){ adapter.notifyDataSetChanged(); adapter = new NewsListAdapter(MainActivity.this, list,2); right.setImageDrawable(getResources().getDrawable(R.drawable.lv_icon)); GridLayoutManager manager = new GridLayoutManager(MainActivity.this, 2); re_view.setLayoutManager(manager); re_view.setAdapter(adapter); flag = false; }else { adapter.notifyDataSetChanged(); adapter = new NewsListAdapter(MainActivity.this, list,1); right.setImageDrawable(getResources().getDrawable(R.drawable.grid_icon)); LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this); re_view.setLayoutManager(manager); re_view.setAdapter(adapter); flag = true; } return false; } }); mpull.setLayoutMode(MODE_APPEND);//设置模板 mpull.setRefreshListener(new BaseRefreshListener() { @Override public void refresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { page=1; presenter.getNews(show,page); mpull.finishRefresh(); } }, 3000); } @Override public void loadMore() { new Handler().postDelayed(new Runnable() { @Override public void run() { page++; presenter.getNews(show,page); mpull.finishLoadMore(); } }, 3000 ); } }); } @Override public void success(String tag, List<JavaBean.DataBean> news) { list.clear(); list.addAll(news); adapter.notifyDataSetChanged(); adapter.setOnItemClickListener(new NewsListAdapter.OnItemClickListener() { @Override public void onClick(int position) { Intent intent = new Intent(MainActivity.this, Main2Activity.class); String url = list.get(position).getDetailUrl(); intent.putExtra("url",url); startActivity(intent); } @Override public void onLongClick(int position) { } }); } @Override public void failed(String tag, Exception e) { } }主包下Main2Activity类
package samsung.com.searchproduct; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class Main2Activity extends AppCompatActivity { private WebView web; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); String url = intent.getStringExtra("url"); web = (WebView) findViewById(R.id.web); web.loadUrl(url); web.setWebViewClient(new WebViewClient()); WebSettings settings = web.getSettings(); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setJavaScriptEnabled(true); } }主包里adapter包下NewsListAdapter类
package samsung.com.searchproduct.adapter; import android.content.Context; import android.graphics.Paint; 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 java.util.List; import samsung.com.searchproduct.R; import samsung.com.searchproduct.bean.JavaBean; public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.ViewHolder> { private Context context; private List<JavaBean.DataBean> list; private int count; public NewsListAdapter(Context context, List<JavaBean.DataBean> list, int count) { this.context = context; this.list = list; this.count = count; } private OnItemClickListener mOnItemClickListener; public interface OnItemClickListener{ void onClick( int position); void onLongClick( int position); } public void setOnItemClickListener(OnItemClickListener onItemClickListener ){ this. mOnItemClickListener=onItemClickListener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (count==1){ View v = View.inflate(context, R.layout.item, null); ViewHolder holder = new ViewHolder(v); return holder; }else if (count==2){ View v = View.inflate(context, R.layout.item, null); ViewHolder holder = new ViewHolder(v); return holder; } return null; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { String[] split = list.get(position).getImages().split("[|]"); Glide.with(context).load(split[0]).into(holder.item_img); holder.item_bargainPrice.setText("原价:¥"+list.get(position).getBargainPrice()); holder.item_bargainPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //下划线 holder.item_bargainPrice.getPaint().setAntiAlias(true);//抗锯齿 holder.item_price.setText("折扣价:¥"+list.get(position).getPrice()); holder.item_title.setText(list.get(position).getTitle()); if( mOnItemClickListener!= null){ holder.itemView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onClick(position); } }); holder. itemView.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { mOnItemClickListener.onLongClick(position); return false; } }); } } @Override public int getItemCount() { if (list==null){ return 0; } return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private final ImageView item_img; private final TextView item_price; private final TextView item_bargainPrice; private final TextView item_title; public ViewHolder(View itemView) { super(itemView); item_img = itemView.findViewById(R.id.item_img); item_title = itemView.findViewById(R.id.item_title); item_price = itemView.findViewById(R.id.item_price); item_bargainPrice = itemView.findViewById(R.id.item_bargainPrice); } } }主包里bean包下JavaBean类
package samsung.com.searchproduct.bean; import java.util.List; /** * Created by samsung on 2017/12/9. */ public class JavaBean { /** * msg : 查询成功 * code : 0 * data : [{"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":77,"price":38999.99,"pscid":40,"salenum":7757,"sellerid":21,"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"},{"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":76,"price":37999.99,"pscid":40,"salenum":6868,"sellerid":20,"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":69,"price":16999,"pscid":40,"salenum":6645,"sellerid":13,"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":61,"price":14999,"pscid":40,"salenum":5535,"sellerid":5,"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":75,"price":36999,"pscid":40,"salenum":5454,"sellerid":19,"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-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":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"},{"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"}] * 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: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 : 77 * price : 38999.99 * pscid : 40 * salenum : 7757 * sellerid : 21 * 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; } } }主包里callback包下CallBack接口
package samsung.com.searchproduct.callback; /** * Created by samsung on 2017/12/9. */ public interface CallBack { void onSuccess(String tag,Object o); void onFailed(String tag,Exception e); }主包里httputils包下HttpUtils类
package samsung.com.searchproduct.httputils; import android.os.Handler; import android.text.TextUtils; import java.io.IOException; import java.util.Map; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import samsung.com.searchproduct.callback.CallBack; import samsung.com.searchproduct.utils.GsonUtils; /** * Created by Wangrx on 2017/11/13. */ public class HttpUtils { private static Handler handler = new Handler(); private static volatile HttpUtils instance; private HttpUtils(){ } public static HttpUtils getInstance(){ if (null==instance){ synchronized (HttpUtils.class){ if (instance == null){ instance = new HttpUtils(); } } } return instance; } public void get(String url, Map<String,String> map, final CallBack callBack, final Class cls, final String tag){ if (TextUtils.isEmpty(url)){ return; } StringBuffer sb = new StringBuffer(); sb.append(url); if (url.contains("?")){ if (url.indexOf("?")==url.length()-1){ }else { sb.append("&"); } }else { sb.append("?"); } for (Map.Entry<String,String> entry:map.entrySet()){ sb.append(entry.getKey()) .append("=") .append(entry.getValue()) .append("&"); } if (sb.indexOf("&")!=-1){ sb.deleteCharAt(sb.lastIndexOf("&")); } OkHttpClient client = new OkHttpClient .Builder() .addInterceptor(new LoggingInterceptor()) .build(); Request request = new Request.Builder() .get() .url(sb.toString()) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { callBack.onFailed(tag,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() { Object o; if (TextUtils.isEmpty(result)){ o = null; }else { o = GsonUtils.getInstance().fromJson(result, cls); } callBack.onSuccess(tag,o); } }); } }); } }主包里httputils包下INewsView接口
package samsung.com.searchproduct.httputils; import java.util.List; import samsung.com.searchproduct.bean.JavaBean; /** * Created by samsung on 2017/12/9. */ public interface INewsView { void success(String tag, List<JavaBean.DataBean> list); void failed(String tag,Exception e); }主包里httputils包下LoggingInterceptor类
package samsung.com.searchproduct.httputils; import java.io.IOException; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; /** * Created by 笔片 on 2017/10/16. * 网络拦截器 */ public class LoggingInterceptor implements Interceptor{ @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); HttpUrl url=original.url().newBuilder() .addQueryParameter("source","android") .build(); //添加请求头 Request request = original.newBuilder() .url(url) .build(); return chain.proceed(request); } }主包里presenter包里NewsPresenter类
package samsung.com.searchproduct.presenter; import java.util.HashMap; import java.util.List; import java.util.Map; import samsung.com.searchproduct.bean.JavaBean; import samsung.com.searchproduct.callback.CallBack; import samsung.com.searchproduct.httputils.HttpUtils; import samsung.com.searchproduct.httputils.INewsView; /** * Created by samsung on 2017/12/9. */ public class NewsPresenter { private INewsView inv; public void attachView(INewsView inv){ this.inv = inv; } public void getNews(String show,int page){ Map<String, String> map = new HashMap<>(); map.put("keywords",show); map.put("page",page+""); HttpUtils.getInstance().get("http://120.27.23.105/product/searchProducts", map, new CallBack() { @Override public void onSuccess(String tag, Object o) { JavaBean bean = (JavaBean) o; if (bean != null){ List<JavaBean.DataBean> data = bean.getData(); inv.success(tag,data); } } @Override public void onFailed(String tag, Exception e) { inv.failed(tag,e); } }, JavaBean.class,"news"); } public void detachView(){ if (inv!=null){ inv = null; } } }主包里utils包下GsonUtils类
package samsung.com.searchproduct.utils; import com.google.gson.Gson; /** * Created by samsung on 2017/12/9. */ public class GsonUtils { private static Gson instance; private GsonUtils(){ } public static Gson getInstance(){ if (instance==null){ instance = new Gson(); } return instance; } }