搜索物品
来源:互联网 发布:在线配音软件 编辑:程序博客网 时间:2024/04/27 22:50
//依赖
compile 'com.jakewharton:butterknife:8.5.1'compile 'com.google.code.gson:gson:2.8.2'compile 'com.github.bumptech.glide:glide:3.7.0'compile 'com.squareup.okhttp3:okhttp:3.9.0'compile 'com.android.support:recyclerview-v7:26.1.0'annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'//清单
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />//shape002.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#f0f2f5"/> <!--设置按钮四个角为弧形--> <!--android:radius弧形的半径--> <corners android:radius="50dip"/> <!--padding;button里面的文字与button边界的间隔--> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/></shape>//acyivity_main
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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" android:background="#000" tools:context="com.example.gouwu.MainActivity"> <com.example.gouwu.MyTitleView android:id="@+id/titleview" android:layout_width="match_parent" android:layout_height="wrap_content"></com.example.gouwu.MyTitleView></android.support.constraint.ConstraintLayout>//activity_shop_goods
<?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="com.example.gouwu.ShopGoodsActivity"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="商品详情页" android:textColor="#000" android:textSize="25dp" /> <ImageView android:id="@+id/img_main_xq" android:layout_width="match_parent" android:layout_height="300dp" android:layout_below="@+id/textView2" android:layout_marginTop="19dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/tv_main_xq_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/img_main_xq" android:layout_marginTop="17dp" android:text="asdasdasdsa" android:textColor="#000" android:textSize="15dp" /> <TextView android:id="@+id/tv_main_xq_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_main_xq_title" android:layout_marginTop="20dp" android:text="qqqqqqqqqqqq" android:textColor="#f5070f" android:textSize="15dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <Button android:id="@+id/btn_main_xq" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ed0707" android:text="加入购物车" /> </LinearLayout></RelativeLayout>//activity_sou_goods
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" android:orientation="vertical" tools:context="com.example.gouwu.SouGoodsActivity"> <TextView android:id="@+id/tv_ss_null" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:text="" android:textColor="#f90505" android:textSize="25sp" /> <android.support.v7.widget.RecyclerView android:id="@+id/ss_recycle" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></LinearLayout>//activity_sou_suo
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" android:orientation="vertical" tools:context="com.example.gouwu.SousuoActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tv_ss_close" android:layout_width="20dp" android:layout_height="20dp" android:background="@drawable/houtui" android:gravity="center" android:textSize="20dp" /> <EditText android:id="@+id/ed_ss_ss" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shape002" android:gravity="center" android:hint="输入搜索的内容" /> <TextView android:id="@+id/btn_ss_ss" android:layout_width="70dp" android:layout_height="wrap_content" android:gravity="center" android:textColor="#000" android:text="搜索" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#6109090a" /> <ListView android:id="@+id/ss_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_weight="1"></ListView> <Button android:id="@+id/btn_ss_clear" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000" android:text="清空搜索记录" /></LinearLayout>//goods_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/img_item_zfl" android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/zfl_item_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="title" android:textColor="#000" android:textSize="15dp" /> <TextView android:id="@+id/zfl_item_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="价格:" android:textColor="#fa0000" /> </LinearLayout></LinearLayout>//sou_list
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:padding="10dp" android:layout_height="match_parent"> <TextView android:id="@+id/fl_list_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp" android:text="asdasdsa"/></LinearLayout>//title_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"> <LinearLayout android:layout_width="50dp" android:layout_height="60dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="8dp" /> <Button android:id="@+id/saoyisao" android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center" android:background="@drawable/jd_title_qrcode" /> <TextView android:layout_width="30dp" android:layout_height="12dp" android:layout_gravity="center" android:text="扫一扫" android:textColor="#ffffff" android:textSize="10sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="54dp" android:layout_marginBottom="8dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="8dp" android:background="@drawable/jd_title_text" /> <Button android:id="@+id/home_title_btn" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" android:layout_alignTop="@+id/imageView" android:background="#00000000" android:text="多功能插座" android:textSize="10dp" /> </RelativeLayout> </LinearLayout> <LinearLayout android:layout_width="50dp" android:layout_height="60dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="8dp" /> <Button android:id="@+id/xiaoxi" android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center" android:background="@drawable/jd_title_msg" /> <TextView android:layout_width="30dp" android:layout_height="12dp" android:layout_gravity="center" android:gravity="center" android:text="消息" android:textColor="#ffffff" android:textSize="10sp" /> </LinearLayout></LinearLayout>//MyTitleView
package com.example.gouwu;import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;public class MyTitleView extends LinearLayout { private static onTitleLinsenter onTitleLinsenter; public interface onTitleLinsenter { void setSaoYiSao(View view); void setTitleBtn(View view); void setXiaoXi(View view); } public void setOnTitleLinsenter(MyTitleView.onTitleLinsenter onTitleLinsenter) { MyTitleView.onTitleLinsenter = onTitleLinsenter; } public MyTitleView(Context context) { this(context, null); } public MyTitleView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public MyTitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs, defStyleAttr); } private void initView(final Context context, AttributeSet attrs, int defStyleAttr) { View view = View.inflate(context, R.layout.title_item, this); Button saoyisao = view.findViewById(R.id.saoyisao); Button titlebtn = view.findViewById(R.id.home_title_btn); Button xiaoxi = view.findViewById(R.id.xiaoxi); saoyisao.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onTitleLinsenter.setSaoYiSao(v); } }); titlebtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onTitleLinsenter.setTitleBtn(v); } }); xiaoxi.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onTitleLinsenter.setXiaoXi(v); } }); }}//MainActivity
package com.example.gouwu;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private MyTitleView titleview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); titleview = findViewById(R.id.titleview); titleview.setOnTitleLinsenter(new MyTitleView.onTitleLinsenter() { @Override public void setSaoYiSao(View view) { Toast.makeText(MainActivity.this, "扫一扫", Toast.LENGTH_SHORT).show(); } @Override public void setTitleBtn(View view) { Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this, SousuoActivity.class); startActivity(intent); } @Override public void setXiaoXi(View view) { Toast.makeText(MainActivity.this, "消息", Toast.LENGTH_SHORT).show(); } }); }}//ShopGoodsActivity
package com.example.gouwu;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import com.bumptech.glide.Glide;import com.example.gouwu.bean.ShopGoodsBean;import com.example.gouwu.bean.ZhuCeBean;import com.example.gouwu.interfaces.IView;import com.example.gouwu.presenter.NewsPresenter;import java.util.HashMap;import java.util.Map;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;public class ShopGoodsActivity extends AppCompatActivity implements IView { @BindView(R.id.textView2) TextView textView2; @BindView(R.id.img_main_xq) ImageView imgMainXq; @BindView(R.id.tv_main_xq_title) TextView tvMainXqTitle; @BindView(R.id.tv_main_xq_price) TextView tvMainXqPrice; @BindView(R.id.btn_main_xq) Button btnMainXq; private NewsPresenter presenter; private int pid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_shop_goods); ButterKnife.bind(this); Intent intent = getIntent(); pid = intent.getIntExtra("pid", 0); presenter = new NewsPresenter(); presenter.attachView(this); Map<String, String> map = new HashMap<>(); map.put("pid", pid + ""); presenter.get("http://120.27.23.105/product/getProductDetail", map, "goodsxq", ShopGoodsBean.class); } @OnClick(R.id.btn_main_xq) public void onViewClicked() { Map<String, String> map = new HashMap<>(); map.put("uid", "2606"); map.put("pid", pid + ""); presenter.post("http://120.27.23.105/product/addCart", map, "addgoods", ZhuCeBean.class); } @Override public void success(String tag, Object o) { if (o != null && tag.equals("goodsxq")) { ShopGoodsBean shopGoods = (ShopGoodsBean) o; ShopGoodsBean.DataBean data = shopGoods.getData(); Glide.with(this).load(data.getImages().split("\\|")[0]).into(imgMainXq); tvMainXqTitle.setText(data.getTitle()); tvMainXqPrice.setText("商品价格: " + data.getPrice() + ""); } if (o != null && tag.equals("addgoods")) { ZhuCeBean zhuCeBean = (ZhuCeBean) o; String code = zhuCeBean.getCode(); if (code.equals("0")) { Toast.makeText(this, "添加购物车成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "添加购物车失败", Toast.LENGTH_SHORT).show(); } } } @Override public void failed(String tag, Exception e) { }}//SouGoodsActivity
package com.example.gouwu;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.widget.TextView;import com.example.gouwu.adapter.MySouGoodsAdapter;import com.example.gouwu.bean.SouGoodsBean;import com.example.gouwu.interfaces.IView;import com.example.gouwu.presenter.NewsPresenter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import butterknife.BindView;import butterknife.ButterKnife;public class SouGoodsActivity extends AppCompatActivity implements IView { @BindView(R.id.ss_recycle) RecyclerView ssRecycle; @BindView(R.id.tv_ss_null) TextView tvSsNull; private NewsPresenter presenter; List<SouGoodsBean.DataBean> list = new ArrayList<>(); private MySouGoodsAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_sou_goods); ButterKnife.bind(this); Intent intent = getIntent(); String keywords = intent.getStringExtra("keywords"); presenter = new NewsPresenter(); presenter.attachView(this); Map<String, String> map = new HashMap<>(); map.put("keywords", keywords); presenter.get("http://120.27.23.105/product/searchProducts", map, "sougoods", SouGoodsBean.class); ssRecycle.setLayoutManager(new LinearLayoutManager(this)); adapter = new MySouGoodsAdapter(list, this, this); ssRecycle.setAdapter(adapter); } @Override public void success(String tag, Object o) { if (o != null && tag.equals("sougoods")) { SouGoodsBean souGoodsBean = (SouGoodsBean) o; List<SouGoodsBean.DataBean> data = souGoodsBean.getData(); list.addAll(data); if (list.size() == 0) { tvSsNull.setText("暂无此类商品"); } adapter.notifyDataSetChanged(); } } @Override public void failed(String tag, Exception e) { } public void ziShopGoods(int pid) { Intent intent = new Intent(this, ShopGoodsActivity.class); intent.putExtra("uid", 2606); intent.putExtra("pid", pid); startActivity(intent); }}//SousuoActivity
package com.example.gouwu;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.example.gouwu.adapter.MySouListAdapter;import java.util.ArrayList;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;public class SousuoActivity extends AppCompatActivity { @BindView(R.id.tv_ss_close) TextView tvSsClose; @BindView(R.id.ed_ss_ss) EditText edSsSs; @BindView(R.id.btn_ss_ss) TextView btnSsSs; @BindView(R.id.ss_list) ListView ssList; @BindView(R.id.btn_ss_clear) Button btnSsClear; List<String> arr = new ArrayList<>(); private MySouListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_sou_suo); ButterKnife.bind(this); adapter = new MySouListAdapter(this, arr); ssList.setAdapter(adapter); } @OnClick({R.id.tv_ss_close, R.id.btn_ss_ss, R.id.btn_ss_clear}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.tv_ss_close: finish(); break; case R.id.btn_ss_ss: String trim = edSsSs.getText().toString().trim(); if (TextUtils.isEmpty(trim)) { Toast.makeText(this, "请输入搜索内容", Toast.LENGTH_SHORT).show(); } else { arr.add(trim); Intent intent = new Intent(this, SouGoodsActivity.class); intent.putExtra("keywords", trim); startActivity(intent); edSsSs.setText(""); adapter.notifyDataSetChanged(); } break; case R.id.btn_ss_clear: arr.clear(); adapter.notifyDataSetChanged(); break; } }}//MySouGoodsAdapter
package com.example.gouwu.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 com.example.gouwu.R;import com.example.gouwu.SouGoodsActivity;import com.example.gouwu.bean.SouGoodsBean;import java.util.List;public class MySouGoodsAdapter extends RecyclerView.Adapter<MySouGoodsAdapter.ViewHodler> { List<SouGoodsBean.DataBean> list; Context context; SouGoodsActivity souGoodsActivity; public MySouGoodsAdapter(List<SouGoodsBean.DataBean> list, Context context, SouGoodsActivity souGoodsActivity) { this.list = list; this.context = context; this.souGoodsActivity = souGoodsActivity; } @Override public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate(context, R.layout.goods_item, null); ViewHodler hodler = new ViewHodler(inflate); return hodler; } @Override public void onBindViewHolder(ViewHodler holder, final int position) { Glide.with(context).load(list.get(position).getImages().split("\\|")[0]).into(holder.images); holder.title.setText(list.get(position).getTitle()); holder.price.setText(list.get(position).getPrice() + ""); holder.title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { souGoodsActivity.ziShopGoods(list.get(position).getPid()); } }); holder.price.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { souGoodsActivity.ziShopGoods(list.get(position).getPid()); } }); holder.images.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { souGoodsActivity.ziShopGoods(list.get(position).getPid()); } }); } @Override public int getItemCount() { if (list != null) { return list.size(); } return 0; } class ViewHodler extends RecyclerView.ViewHolder { TextView title, price; ImageView images; public ViewHodler(View itemView) { super(itemView); images = itemView.findViewById(R.id.img_item_zfl); title = itemView.findViewById(R.id.zfl_item_title); price = itemView.findViewById(R.id.zfl_item_price); } }}//MySouListAdapter
package com.example.gouwu.adapter;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import com.example.gouwu.R;import java.util.List;public class MySouListAdapter extends BaseAdapter { Context context; List<String> list; public MySouListAdapter(Context context, List<String> list) { this.context = context; this.list = list; } @Override public int getCount() { if (list == null) { return 0; } return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = View.inflate(context, R.layout.sou_list, null); holder.name = convertView.findViewById(R.id.fl_list_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.name.setText(list.get(position)); return convertView; } class ViewHolder { TextView name; }}//presenter
//NewsPresenter
package com.example.gouwu.presenter;import com.example.gouwu.interfaces.CallBack;import com.example.gouwu.interfaces.IView;import com.example.gouwu.utils.HttpUtils;import java.util.Map;public class NewsPresenter { private IView inv; public void attachView(IView inv) { this.inv = inv; } public void get(String url, Map<String, String> map, String tag, Class cla) { HttpUtils.getInstance().get(url, map, new CallBack() { @Override public void onSuccess(String tag, Object o) { if (o != null) { inv.success(tag, o); } } @Override public void onFailed(String tag, Exception e) { inv.failed(tag, e); } }, cla, tag); } public void post(String url, Map<String, String> map, String tag, Class cla) { HttpUtils.getInstance().post(url, map, new CallBack() { @Override public void onSuccess(String tag, Object o) { if (o != null) { inv.success(tag, o); } } @Override public void onFailed(String tag, Exception e) { inv.failed(tag, e); } }, cla, tag); } public void deleteView() { if (inv != null) { inv = null; } }}//utils
GsonUtils
package com.example.gouwu.utils;import com.google.gson.Gson;public class GsonUtils { private static Gson gson; public static Gson getInstance() { if (gson == null) { gson = new Gson(); } return gson; }}//HttpUtils
package com.example.gouwu.utils;import android.os.Handler;import android.text.TextUtils;import com.example.gouwu.interfaces.CallBack;import java.io.IOException;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.FormBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class HttpUtils { private static volatile HttpUtils instance; private OkHttpClient client; Handler handler = new Handler(); private HttpUtils() { client = new OkHttpClient.Builder().addInterceptor(new Logger()).build(); } 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 cla, final String tag) { if (TextUtils.isEmpty(url)) { return; } StringBuffer sb = new StringBuffer(); sb.append(url); if (!map.isEmpty()) { 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().trim()) .append("&"); } if (sb.indexOf("&") != -1) { sb.deleteCharAt(sb.lastIndexOf("&")); } } final 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, cla); } callBack.onSuccess(tag, o); } }); } }); } public void post(String url, Map<String, String> map, final CallBack callBack, final Class cla, final String tag) { if (TextUtils.isEmpty(url)) { return; } OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Logger()).build(); FormBody.Builder BodyBuilder = new FormBody.Builder(); if (!map.isEmpty() && map != null) { for (Map.Entry<String, String> entry : map.entrySet()) { BodyBuilder.add(entry.getKey(), entry.getValue()); } } FormBody body = BodyBuilder.build(); final Request request = new Request.Builder() .post(body) .url(url) .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, cla); } callBack.onSuccess(tag, o); } }); } }); }}//Logger
package com.example.gouwu.utils;import java.io.IOException;import okhttp3.HttpUrl;import okhttp3.Interceptor;import okhttp3.Request;import okhttp3.Response;public class Logger 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); }}//CallBack
package com.example.gouwu.interfaces;public interface CallBack { void onSuccess(String tag, Object o); void onFailed(String tag, Exception e);}//IView
package com.example.gouwu.interfaces;public interface IView { void success(String tag, Object o); void failed(String tag, Exception e);}//bean
//ShopGoodsBean
package com.example.gouwu.bean;public class ShopGoodsBean { /** * msg : * seller : {"description":"我是商家19","icon":"http://120.27.23.105/images/icon.png","name":"商家19","productNums":999,"score":5,"sellerid":19} * code : 0 * data : {"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":0,"pid":3,"price":198,"pscid":1,"salenum":1234,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"} */ 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 : 我是商家19 * icon : http://120.27.23.105/images/icon.png * name : 商家19 * productNums : 999 * score : 5.0 * sellerid : 19 */ 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 : 111.99 * createtime : 2017-10-03T23:53:28 * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg * itemtype : 0 * pid : 3 * price : 198.0 * pscid : 1 * salenum : 1234 * sellerid : 19 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ 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; } }}//SouGoodsBean
package com.example.gouwu.bean;import java.util.List;public class SouGoodsBean { /** * msg : 查询成功 * code : 0 * data : [{"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-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":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: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":60,"price":13888,"pscid":40,"salenum":466,"sellerid":4,"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-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":62,"price":15999,"pscid":40,"salenum":43,"sellerid":6,"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"},{"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-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.0 * 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)银 */ 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; } }}//ZhuCeBean
package com.example.gouwu.bean;public class ZhuCeBean { /** * msg : 天呢!用户已注册 * code : 1 */ private String msg; private String code; 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; }}
阅读全文
0 0
- 搜索物品
- 物品构建
- 泛化物品
- 魔法物品
- 取物品
- 推荐物品
- 放物品
- uva1629 切方格使得每个方格中只有一个物品,求最少切割费用 记忆化搜索
- 爱人的贵重物品
- wow物品分类
- 最喜欢的物品
- 地震预备物品zz
- P08: 泛化物品
- 增加物品或怪物
- 【動態規劃】物品裝箱問題
- Javascript物品分拣小游戏
- 基于物品的CF
- 实体物品测试
- 基于Vue + Node.js + MongoDB的图片上传组件,实现图片的预览和删除
- 利用容器map配合pair实现字符串中字符的计数
- scikit-learn线性回归源码
- Python ssh 远程执行shell命令
- git常用操作
- 搜索物品
- 关于数据库分表分库的一次分享
- STM32学习笔记9——stm32L072 SD卡程序移植记录
- spring boot actuator简述
- xlua 热更新
- [POJ1655] Balancing Act
- 编程素养015
- MVC学习开始
- struts2框架基础web环境搭建