p层
package com.example.lenovo.lx3.presenter;
import android.os.Handler;
import com.example.lenovo.lx3.contract.IContract;
import com.example.lenovo.lx3.model.IModel;
import com.example.lenovo.lx3.net.RequestCallback;
import com.example.lenovo.lx3.utils.ValidatorUtil;
import java.util.HashMap;
public class IPresenter extends IContract.Ipresenter {
private IModel iModel;
private IContract.IView iView;
Handler handler=new Handler();
public IPresenter(IContract.IView iView) {
this.iModel = new IModel();
this.iView = iView;
}
@Override
public void show(String api, HashMap<String, String> params) {
if(params!=null){
final String phone = params.get("mobile");
if(!ValidatorUtil.isMobile(phone)){
if(iView!=null){
iView.onMobileError("手机号不合法");
}
return;
}
}
if(iModel!=null){
iModel.show(api, params, new RequestCallback() {
@Override
public void success(final String result) {
if(iView!=null){
handler.post(new Runnable() {
@Override
public void run() {
iView.onSuccess(result);
}
});
}
}
@Override
public void failure(final String error) {
if(iView!=null){
handler.post(new Runnable() {
@Override
public void run() {
iView.onFailure(error);
}
});
}
}
});
}
}
@Override
public void showget(String api, HashMap<String, String> params) {
if(params!=null){
final String phone = params.get("phone");
if(!ValidatorUtil.isMobile("phone")){
if(iView!=null){
iView.onMobileError("手机号不合法");
}
return;
}
}
if(iModel!=null){
iModel.showget(api, params, new RequestCallback() {
@Override
public void success(final String result) {
if(iView!=null){
handler.post(new Runnable() {
@Override
public void run() {
iView.onSuccess(result);
}
});
}
}
@Override
public void failure(final String error) {
if(iView!=null){
handler.post(new Runnable() {
@Override
public void run() {
iView.onFailure(error);
}
});
}
}
});
}
}
public void destroy(){
if(iView!=null){
iView=null;
}
}
}
M层
package com.example.lx1.model;
import android.os.Handler;
import android.text.TextUtils;
import com.example.lx1.api.UserApi;
import com.example.lx1.contract.IShowContract;
import com.example.lx1.entity.User;
import com.example.lx1.net.OkhttpCallback;
import com.example.lx1.net.OkhttpUtils;
import com.example.lx1.net.RequestCallback;
import com.google.gson.Gson;
import java.util.HashMap;
public class ShowModel implements IShowContract.IShowModel {
Handler handler=new Handler();
@Override
public void show(HashMap<String, String> params, final RequestCallback requestCallback) {
OkhttpUtils.getmInstance().doPost(UserApi.UserShow, params, new OkhttpCallback() {
@Override
public void onFailure(String msg) {
if(requestCallback!=null){
handler.post(new Runnable() {
@Override
public void run() {
requestCallback.onFailUre(“网络不稳定,请稍后再试”);
}
});
}
}
@Override
public void onSuccess(String result) {
if(!TextUtils.isEmpty(result)){
requestcall(result,requestCallback);
}
}
});
}
private void requestcall(String result, final RequestCallback requestCallback) {
final User user = new Gson().fromJson(result, User.class);
if(requestCallback!=null){
handler.post(new Runnable() {
@Override
public void run() {
requestCallback.onSuccess(user);
}
});
}
}
}
okhttputils
package com.example.lx1.net;
import android.os.Handler;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OkhttpUtils {
Handler handler=new Handler();
private final OkHttpClient okHttpClient;
public static OkhttpUtils mInstance;
public OkhttpUtils() {
final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(5,TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5,TimeUnit.SECONDS)
.build();
}
public static OkhttpUtils getmInstance() {
if(mInstance==null){
synchronized (OkhttpUtils.class){
mInstance=new OkhttpUtils();
}
}
return mInstance;
}
public void doPost(String url, HashMap<String,String> params, final OkhttpCallback okhttpCallback){
final FormBody.Builder formbody = new FormBody.Builder();
for (Map.Entry<String, String> map : params.entrySet()) {
formbody.add(map.getKey(),map.getValue());
}
final Request request = new Request.Builder().url(url).post(formbody.build()).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if(okhttpCallback!=null){
handler.post(new Runnable() {
@Override
public void run() {
okhttpCallback.onFailure("网络不稳定");
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String result = response.body().string();
final int code = response.code();
if(okhttpCallback!=null){
if(code==200){
handler.post(new Runnable() {
@Override
public void run() {
okhttpCallback.onSuccess(result);
}
});
}
}
}
});
}
public void cancelAllTask(){
if(okHttpClient!=null){
okHttpClient.dispatcher().cancelAll();
}
}
}
契约类
package com.example.lx1.contract;
import com.example.lx1.entity.User;
import com.example.lx1.net.RequestCallback;
import java.util.HashMap;
public interface IShowContract {
public abstract class IShowpresenter{
public abstract void show(HashMap<String,String> params);
}
interface IShowModel{
void show(HashMap<String,String> params, RequestCallback requestCallback);
}
interface IShowView{
void onFailUre(String msg);
void onSuccess(User user);
void onSuccessMsg(String msg);
}
}
adapter
package com.example.lx1.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.lx1.R;
import com.example.lx1.entity.User;
import java.util.ArrayList;
import java.util.List;
public class MAdapter extends RecyclerView.Adapter<MAdapter.ViewHolder> {
private Context context;
private List<User.DataBean> list;
public MAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<User.DataBean> list) {
if(list!=null) {
this.list = list;
}
notifyDataSetChanged();
}
@NonNull
@Override
public MAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
final View view = LayoutInflater.from(context).inflate(R.layout.linear, viewGroup, false);
final ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MAdapter.ViewHolder viewHolder, int i) {
final User.DataBean dataBean = list.get(i);
final String[] split = dataBean.getImages().split("!");
Glide.with(context).load(split[0]).into(viewHolder.image);
viewHolder.title.setText(dataBean.getTitle());
viewHolder.price.setText(dataBean.getPrice()+"");
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView image;
private TextView title;
private TextView price;
public ViewHolder(@NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.image);
title=itemView.findViewById(R.id.title);
price=itemView.findViewById(R.id.price);
}
}
}
actvity
package com.example.lx1.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.lx1.R;
import com.example.lx1.adapter.MAdapter;
import com.example.lx1.contract.IShowContract;
import com.example.lx1.entity.User;
import com.example.lx1.net.OkhttpUtils;
import com.example.lx1.presenter.ShowPresenter;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IShowContract.IShowView {
private Button btn_search;
private EditText ed_title;
private RecyclerView gv;
private ShowPresenter presenter;
private MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_search = findViewById(R.id.btn_search);
ed_title = findViewById(R.id.ed_title);
gv = findViewById(R.id.gv);
gv.setLayoutManager(new LinearLayoutManager(this));
presenter = new ShowPresenter(this);
mAdapter = new MAdapter(this);
btn_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String keywords =ed_title.getText().toString();
HashMap<String,String> params=new HashMap<>();
params.put("keywords",keywords);
params.put("page",1+"");
presenter.show(params);
}
});
}
@Override
public void onFailUre(String msg) {
}
@Override
public void onSuccess(User user) {
final List<User.DataBean> data = user.getData();
mAdapter.setList(data);
gv.setAdapter(mAdapter);
}
@Override
public void onSuccessMsg(String msg) {
}
@Override
protected void onDestroy() {
super.onDestroy();
OkhttpUtils.getmInstance().cancelAllTask();
presenter.destroy();
}
}