-----------------------依赖------------------------------
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.android.support:design:26+' compile 'com.google.code.gson:gson:2.2.4' compile 'com.github.userswlwork:pull-to-refresh:1.0.0' compile 'com.youth.banner:banner:1.4.9' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.squareup.okio:okio:1.5.0' compile 'com.github.bumptech.glide:glide:3.6.1'
--------------《Model》----------------------
<--IModel
package com.example.mvp_logintozhuce.Model; import com.example.mvp_logintozhuce.Bean.User; public interface IModel { //登录和注册的方法和各自监听 void Login(User user,ILoginListener iLoginListener); void Zhuce(User user,IZhuceListener zhuceListener); }<--IloadModel
package com.example.mvp_logintozhuce.Model; //数据加载 public interface IloadModel { void showData(String url, OkHttpLoadListener okHttpLoadListener); }
<--登录的监听
package com.example.mvp_logintozhuce.Model; /** * 登录监听 */ public interface ILoginListener { void LoginSuccess(String json); void LoginError(String error); }
<--注册的监听
package com.example.mvp_logintozhuce.Model; /** * 注册监听 */ public interface IZhuceListener { //注册监听 void ZhuceSuccess(); void ZhuceError(); }
<--ImodelImpl实现类
package com.example.mvp_logintozhuce.Model; import com.example.mvp_logintozhuce.Bean.User; import com.example.mvp_logintozhuce.Utils.HttpConfig; import com.example.mvp_logintozhuce.Utils.OkHttpUtils; import org.json.JSONException; import org.json.JSONObject; /** * 实现类 */ public class ModelImpl implements IModel, IloadModel { private int page = 0; //model的实现类 private static final String TAG = "ModelImpl"; @Override public void Login(User user, final ILoginListener iLoginListener) { //获取账号密码 String mobile = user.getMobile(); String password = user.getPassword(); //请求网络 OkHttpUtils okHttpUtils = new OkHttpUtils(); okHttpUtils.setOkHttpLoadListener(new OkHttpLoadListener() { @Override public void loadSuccess(String json) { //成功就解析数据 try { JSONObject object = new JSONObject(json); String code = object.getString("code"); if (code.equals("0")) { iLoginListener.LoginSuccess(json); } else { iLoginListener.LoginError("失败"); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void loadError(String error) { iLoginListener.LoginError(error); } }); //获取url接口 - 登录 okHttpUtils.okHttpGet(HttpConfig.login_url + "?mobile=" + mobile + "&password=" + password); } @Override public void Zhuce(User user, final IZhuceListener zhuceListener) { String mobile = user.getMobile(); String password = user.getPassword(); //OKhttp OkHttpUtils okHttpUtils = new OkHttpUtils(); okHttpUtils.setOkHttpLoadListener(new OkHttpLoadListener() { @Override public void loadSuccess(String json) { try { JSONObject object = new JSONObject(json); String code = object.getString("code"); if (code.equals("0")) { zhuceListener.ZhuceSuccess(); } else { zhuceListener.ZhuceError(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void loadError(String error) { zhuceListener.ZhuceError(); } }); okHttpUtils.okHttpGet(HttpConfig.reg_url + "?mobile=" + mobile + "&password=" + password); } //加载数据. @Override public void showData(String url, final OkHttpLoadListener okHttpLoadListener) { OkHttpUtils okHttpUtils = new OkHttpUtils(); okHttpUtils.setOkHttpLoadListener(new OkHttpLoadListener() { @Override public void loadSuccess(String json) { okHttpLoadListener.loadSuccess(json); } @Override public void loadError(String error) { okHttpLoadListener.loadError(error); } }); okHttpUtils.okHttpGet(HttpConfig.goods_url); } }
<--网络(okhttp)监听
package com.example.mvp_logintozhuce.Model; /** * 网络监听 */ public interface OkHttpLoadListener { void loadSuccess(String json); void loadError(String error); }
------------------《Precenter》----------------------------
package com.example.mvp_logintozhuce.Precenter; import android.content.Context; import com.example.mvp_logintozhuce.Model.IModel; import com.example.mvp_logintozhuce.Model.IloadModel; import com.example.mvp_logintozhuce.View.IGoodView; import com.example.mvp_logintozhuce.View.Iview; /** * author:Created by Guozhenwei on 2018/1/12. */ public interface IPrecenter { //登录注册相互关联 void login(IModel iModel, Iview iview); void zhuce (IModel iModel,Iview iview); void showData(Context context, IloadModel iloadModel, IGoodView iGoodView); }
<--实现类
package com.example.mvp_logintozhuce.Precenter; import android.content.Context; import android.util.Log; import android.widget.Toast; import com.example.mvp_logintozhuce.Bean.Goods; import com.example.mvp_logintozhuce.Bean.User; import com.example.mvp_logintozhuce.Model.ILoginListener; import com.example.mvp_logintozhuce.Model.IModel; import com.example.mvp_logintozhuce.Model.IZhuceListener; import com.example.mvp_logintozhuce.Model.IloadModel; import com.example.mvp_logintozhuce.Model.OkHttpLoadListener; import com.example.mvp_logintozhuce.Utils.HttpConfig; import com.example.mvp_logintozhuce.View.IGoodView; import com.example.mvp_logintozhuce.View.Iview; import com.google.gson.Gson; import java.util.List; //实现类 public class PrecenterImpl implements IPrecenter { private static final String TAG = "LoginSuccess"; @Override public void login(IModel iModel, final Iview iview) { //这里调用Imodel登录的方法 iModel.Login(new User(iview.getName(), iview.getPwd()), new ILoginListener() { @Override public void LoginSuccess(String json) { //成功就调用view里跳转的方法 iview.jumpActivity(); } @Override public void LoginError(String error) { //失败就调用view里失败的方法 iview.showError(); } }); } //注册的方法 @Override public void zhuce(IModel iModel, final Iview iview) { iModel.Zhuce(new User(iview.getName(), iview.getPwd()), new IZhuceListener() { @Override public void ZhuceSuccess() { iview.jumpActivity(); } @Override public void ZhuceError() { iview.showError(); } }); } @Override public void showData(final Context context, IloadModel iloadModel, final IGoodView iGoodView) { //调用iloadmodel的显示数据的方法 iloadModel.showData(HttpConfig.goods_url, new OkHttpLoadListener() { @Override public void loadSuccess(String json) { Toast.makeText(context,json,Toast.LENGTH_SHORT).show(); Log.d(TAG, "loadSuccess() returned: " + "成功----"); Gson gson = new Gson(); Goods goods = gson.fromJson(json, Goods.class); List<Goods.DataBean> data = goods.getData(); iGoodView.showDataToView(context,data); } @Override public void loadError(String error) { Toast.makeText(context, "失败", Toast.LENGTH_SHORT).show(); } }); } }
-----------------《View》-------------------------------
<--展示商品Activity
package com.example.mvp_logintozhuce.View; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import com.example.mvp_logintozhuce.Bean.Goods; import com.example.mvp_logintozhuce.Model.ModelImpl; import com.example.mvp_logintozhuce.Precenter.PrecenterImpl; import com.example.mvp_logintozhuce.R; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.List; public class GoodsActivity extends AppCompatActivity implements IGoodView { private PullToRefreshListView pull; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_goods); PrecenterImpl precenter = new PrecenterImpl(); precenter.showData(this,new ModelImpl(),this); pull = (PullToRefreshListView) findViewById(R.id.pull); pull.setMode(PullToRefreshBase.Mode.BOTH);//允许加载数据 } private static final String TAG = "ContentActivity"; //展示数据到view @Override public void showDataToView(Context context, List<Goods.DataBean> list) { Log.d(TAG, "showDataToView: "+list.toString()+"!!!!!!!!!!!!!!!!!!!!!"); //添加适配器 MyBaseAdpater myBaseAdpater = new MyBaseAdpater(context,list); pull.setAdapter(myBaseAdpater); myBaseAdpater.notifyDataSetChanged(); } }
<--登录Activity
package com.example.mvp_logintozhuce.View; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.mvp_logintozhuce.Model.ModelImpl; import com.example.mvp_logintozhuce.Precenter.PrecenterImpl; import com.example.mvp_logintozhuce.R; public class MainActivity extends AppCompatActivity implements Iview,View.OnClickListener { private EditText name; private EditText pwd; private Button login_but; private Button zhuce_but; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initViews(); } private void initViews() { name = (EditText) findViewById(R.id.name); pwd = (EditText) findViewById(R.id.pwd); login_but = (Button) findViewById(R.id.login); zhuce_but = (Button) findViewById(R.id.zhuce); login_but.setOnClickListener(this); zhuce_but.setOnClickListener(this); } @Override public String getName() { return name.getText().toString(); } @Override public String getPwd() { return pwd.getText().toString(); } @Override public void jumpActivity() { //成功就跳转 startActivity(new Intent(MainActivity.this,GoodsActivity.class)); } @Override public void showError() { Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show(); } //按钮事件 @Override public void onClick(View view) { switch(view.getId()){ case R.id.login: //回调 PrecenterImpl precenter = new PrecenterImpl(); precenter.login(new ModelImpl(),this); break; case R.id.zhuce: startActivity(new Intent(MainActivity.this,RegActivity.class)); break; } } }
<--注册Activity
package com.example.mvp_logintozhuce.View; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.mvp_logintozhuce.Model.ModelImpl; import com.example.mvp_logintozhuce.Precenter.PrecenterImpl; import com.example.mvp_logintozhuce.R; public class RegActivity extends AppCompatActivity implements Iview,View.OnClickListener { private EditText name2; private EditText pwd2; private Button but; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reg); //初始化控件 initViews(); } private void initViews() { name2 = (EditText) findViewById(R.id.name2); pwd2 = (EditText) findViewById(R.id.pwd2); but = (Button) findViewById(R.id.reg); but.setOnClickListener(this); } @Override public String getName() { return name2.getText().toString(); } @Override public String getPwd() { return pwd2.getText().toString(); } @Override public void jumpActivity() { //注册成功就跳转登录界面 Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show(); startActivity(new Intent(RegActivity.this,MainActivity.class)); } @Override public void showError() { Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show(); } //注册按钮 @Override public void onClick(View view) { PrecenterImpl precenter = new PrecenterImpl(); precenter.zhuce(new ModelImpl(),this); } }
<--展示商品的接口
package com.example.mvp_logintozhuce.View; import android.content.Context; import com.example.mvp_logintozhuce.Bean.Goods; import java.util.List; //展示商品接口 public interface IGoodView { void showDataToView(Context context, List<Goods.DataBean> list); }
<--IView接口
package com.example.mvp_logintozhuce.View; public interface Iview { //获取用户名密码 String getName(); String getPwd(); void jumpActivity();//跳转的方法 void showError();//显示错误 }
<--适配器
package com.example.mvp_logintozhuce.View; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.mvp_logintozhuce.Bean.Goods; import com.example.mvp_logintozhuce.R; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; public class MyBaseAdpater extends BaseAdapter { private Context context; private List<Goods.DataBean> list; private ImageView img1; private ImageView img2; private TextView text; //有参构造 public MyBaseAdpater(Context context, List<Goods.DataBean> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null) { view = View.inflate(context, R.layout.goods, null); } img1 = view.findViewById(R.id.img1); img2 = view.findViewById(R.id.img2); ImageLoader.getInstance().displayImage(list.get(i).getPic_url(),img1); text = (TextView) view.findViewById(R.id.text); text.setText(list.get(i).getNews_title()); return view; } }
---------------------《Utils》---------------------
<--OkHttpUtils
package com.example.mvp_logintozhuce.Utils; import android.os.Handler; import android.os.Message; import com.example.mvp_logintozhuce.Model.OkHttpLoadListener; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class OkHttpUtils { private MyHandler myHandler = new MyHandler(); private OkHttpLoadListener okHttpLoadListener; //get请求 public void okHttpGet(String url) { OkHttpClient client = new OkHttpClient(); Request builder = new Request.Builder().url(url).build(); Call call = client.newCall(builder); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message message = myHandler.obtainMessage(); message.what = 0; message.obj = "失败"; myHandler.sendMessage(message); } @Override public void onResponse(Call call, Response response) throws IOException { Message message = myHandler.obtainMessage(); message.what = 1; message.obj = response.body().string(); myHandler.sendMessage(message); } }); } class MyHandler extends Handler { @Override public void handleMessage(Message msg) { int i = msg.what; switch (i) { case 0: String s = (String) msg.obj; //调用接口 okHttpLoadListener.loadError(s); break; case 1: String json = (String) msg.obj; okHttpLoadListener.loadSuccess(json); break; } } } //接口回调 public void setOkHttpLoadListener(OkHttpLoadListener okHttpLoadListener) { this.okHttpLoadListener = okHttpLoadListener; } }
<--接口
package com.example.mvp_logintozhuce.Utils; /** * author:Created by WangZhiQiang on 2018/1/8. */ public class HttpConfig { public static String login_url = "https://www.zhaoapi.cn/user/login"; public static String reg_url = "https://www.zhaoapi.cn/user/reg"; public static String goods_url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1"; }