RestrofitUtils
public class RestrofitUtils {
private final MyApiService myApiService;
private RestrofitUtils(){
//日志拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.retryOnConnectionFailure(true)
.build();
//初始化retrofit 结合两种操作 一个是gson解析 一个是结合rxjava
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(API.BASE_URL)
.client(okHttpClient)
.build();
myApiService = retrofit.create(MyApiService.class);
}
public static RestrofitUtils getInstance() {
return RetrofitHolder.UTIL;
}
private static class RetrofitHolder {
private static final RestrofitUtils UTIL = new RestrofitUtils();
}
public RestrofitUtils doGet(String url, HashMap<String, String> map) {
//注意与 myApiService的HashMap一样否则类型不匹配
myApiService.doGet(url,map)
//主线程在前
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(observer);
return RestrofitUtils.getInstance();
}
private Observer observer = new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
if (httpListener != null) {
httpListener.onError(e.getMessage());
}
}
@Override
public void onNext(ResponseBody responseBody) {
if (httpListener != null) {
try {
httpListener.onSuccess(responseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
public interface HttpListener {
void onSuccess(String result);
void onError(String error);
}
private HttpListener httpListener;
public void setHttpListener(HttpListener listener) {
this.httpListener = listener;
}
}
UtilsCallBack接口
public interface UtilsCallBack {
void success(T result);
void defeated(T error);
}
MyApp
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
API
public class API {
public static final String BASE_URL = “http://172.17.8.100/small/commodity/v1/”;
//商品展示网址
public static final String SHANG_URL = "findCommodityByKeyword";
//商品详情网址
public static final String XIANGQ_URL = "findCommodityDetailsById";
}
MyApiService接口
public interface MyApiService {
@GET
Observable doGet(@Url String url, @QueryMap HashMap<String,String> map);
}
Model接口
public interface DataModel {
void getData(String url, HashMap<String,String> map, UtilsCallBack utilsCallBack);
}
DataModelImpl
public class DataModelImpl implements DataModel{
@Override
public void getData(String url, HashMap<String, String> map, final UtilsCallBack utilsCallBack) {
RestrofitUtils.getInstance().doGet(url,map).setHttpListener(new RestrofitUtils.HttpListener() {
@Override
public void onSuccess(String result) {
DataBean dataBean = new Gson().fromJson(result, DataBean.class);
utilsCallBack.success(dataBean);
}
@Override
public void onError(String error) {
utilsCallBack.defeated(error);
}
});
}
}
presenter接口
public interface DataPresenter {
void setData(String url, HashMap<String,String> map);
}
DataPresenterImpl
public class DataPresenterImpl implements DataPresenter{
private DataView dataView;
private DataModelImpl dataModel = new DataModelImpl();
public DataPresenterImpl(DataView dataView) {
this.dataView = dataView;
}
@Override
public void setData(String url, HashMap<String, String> map) {
dataModel.getData(url, map, new UtilsCallBack() {
@Override
public void success(Object result) {
dataView.success(result);
}
@Override
public void defeated(Object error) {
dataView.defeated(error);
}
});
}
}
DataView接口
public interface DataView {
void success(T result);
void defeated(T error);
}
Adapter
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyVh> {
private Context context;
private List<DataBean.ResultBean> data;
public DataAdapter(Context context, List<DataBean.ResultBean> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public DataAdapter.MyVh onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.dataadapter, null);
return new MyVh(view);
}
@Override
public void onBindViewHolder(@NonNull DataAdapter.MyVh myVh, int i) {
myVh.simp.setImageURI(data.get(i).getMasterPic());
myVh.tv_name.setText(data.get(i).getCommodityName());
myVh.tv_money.setText("¥"+data.get(i).getPrice());
myVh.tv_shou.setText("已售"+data.get(i).getSaleNum()+"件");
myVh.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public class MyVh extends RecyclerView.ViewHolder {
private SimpleDraweeView simp;
private TextView tv_name;
private TextView tv_money;
private TextView tv_shou;
public MyVh(@NonNull View itemView) {
super(itemView);
simp = itemView.findViewById(R.id.simp);
tv_name = itemView.findViewById(R.id.tv_name);
tv_money = itemView.findViewById(R.id.tv_money);
tv_shou = itemView.findViewById(R.id.tv_shou);
}
}
}
主页面
public class MainActivity extends AppCompatActivity implements View.OnClickListener, DataView {
private EditText edText;
private Button butSou;
private XRecyclerView xrecyclerview;
private DataPresenterImpl dataPresenter;
private String trim;
private DataAdapter dataAdapter;
private ImageView tv_cai;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//资源ID
initView();
dataPresenter = new DataPresenterImpl(this);
}
@Override
public void onClick(View v) {
trim = edText.getText().toString().trim();
HashMap<String, String> map = new HashMap<>();
map.put("keyword",trim);
map.put("page","1");
map.put("count","10");
switch (v.getId()){
case R.id.but_sou:
dataPresenter.setData(API.SHANG_URL,map);
break;
}
}
@Override
public void success(T result) {
if (result instanceof DataBean){
DataBean dataBean=(DataBean) result;
dataAdapter = new DataAdapter(this, dataBean.getResult());
xrecyclerview.setAdapter(dataAdapter);
xrecyclerview.setLayoutManager(new GridLayoutManager(this,2));
}
}
@Override
public void defeated(T error) {
}
//资源ID
private void initView() {
tv_cai = findViewById(R.id.tv_cai);
edText = (EditText) findViewById(R.id.ed_text);
butSou = (Button) findViewById(R.id.but_sou);
xrecyclerview = (XRecyclerView) findViewById(R.id.xrecyclerview);
butSou.setOnClickListener(this);
}
}
主布局
<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=".activity.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageView
android:id="@+id/tv_cai"
android:layout_weight="2"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/cai"/>
<EditText
android:id="@+id/ed_text"
android:hint="请输入您所查询的商品"
android:layout_weight="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/but_sou"
android:text="搜索"
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrecyclerview"
android:layout_width="match_parent"
android:layout_weight="9"
android:layout_height="0dp"/>
</LinearLayout>
次布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simp"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="150dp" />
<TextView
android:layout_gravity="center_horizontal"
android:id="@+id/tv_name"
android:text="标题"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
/>
<LinearLayout
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="150dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_money"
android:textColor="#FFF7112B"
android:text="money"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_shou"
android:text="money"
android:layout_weight="1"
android:layout_marginLeft="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>