MVP的实现,后续更新mvp和databinding结合
来源:互联网 发布:snh48是什么玩意知乎 编辑:程序博客网 时间:2024/06/09 20:58
上图是界面和运行效果,mvp的结构图如下:
presenter类主要处理业务逻辑,view通过接口来和presenter交互减少耦合度,mode主要是存储数据和提供数据。
—–presenter—–
presenter代码
/** * 业务逻辑顶层父类 * Created by Administrator on 2017/9/2. */ public class BasePresenter { Context mContext; public void onAttached(Context mContext) { this.mContext = mContext; } public void onCreat() { } public void onPause() { } public void onResume() { } public void onDestory() { System.out.println("=test=BasePresenter=onDestory"); if (mContext != null) { mContext = null; } }}** * mainActivity的逻辑处理类 * Created by Administrator on 2017/9/2. */ public class MainViewPresenter extends BasePresenter implements HttpUtil.HttpResponse { //创建构造函数,要求是实现了mainViewInterface的子类 //view和presenter的对接扣 String netUrl = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm"; public MainView mainView; public PhoneInfo phoneInfo; public MainViewPresenter(MainView mainView) { this.mainView = mainView; } //逻辑处理,获取数据,点击就事件等 //获取网络数据,传入参数 public void fetchDataFromNet(String phoneNumber) { //校验或者包装参数 if (phoneNumber.length() != 11) { mainView.showToast("请输入正确手机号"); return; } mainView.showLoading(); //下面是okhhtp开始请求数据 HttpUtil httpUtil = new HttpUtil(); HashMap<String, String> map = new HashMap<>(); map.put("tel", phoneNumber); httpUtil.sendHttpGet(map, netUrl); httpUtil.setHttpResponse(this); } @Override public void onFailed(String failure) { mainView.showToast(failure); mainView.hidenLoading(); } @Override public void onSuccess(String result) { mainView.hidenLoading(); //数据解析也在presenter try { result=result.substring(result.indexOf("{"),result.length()); phoneInfo=new Gson().fromJson(result,PhoneInfo.class); } catch (JsonSyntaxException e) { e.printStackTrace(); mainView.showToast("返回结果解析异常"); } //刷新UI mainView.updataCurrentView(); } public PhoneInfo getPhoneInfo(){ return phoneInfo; }}
—–view—–
view层
/** * view的顶级父类,给p提供交互 * Created by Administrator on 2017/9/2. */public interface BaseView { void showLoading(); void hidenLoading();}/** * mainView的接口,v和p都是通过infterface来交互解耦 * Created by Administrator on 2017/9/2. */public interface MainView extends BaseView { //view的方法太多,抽取父类,都是给pren层的 void showToast(String msg); void updataCurrentView();//可以传入参数的}-----activity的代码-----public class MainActivity extends AppCompatActivity implements View.OnClickListener,MainView { private MainViewPresenter mainViewPresenter; private ActivityMainBinding mainBinding; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); //将整个view层都传出去 mainBinding.btnQuery.setOnClickListener(this); mainViewPresenter = new MainViewPresenter(this); mainViewPresenter.onAttached(this); } @Override protected void onDestroy() { super.onDestroy(); mainViewPresenter.onDestory(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_query: String phone=mainBinding.editPhone.getText().toString().trim(); mainViewPresenter.fetchDataFromNet(phone); break; } } @Override public void showLoading() { dialog = new ProgressDialog(this); dialog.show(); } @Override public void hidenLoading() { if (dialog!=null&&dialog.isShowing()) { dialog.dismiss(); } } @Override public void showToast(String msg) { Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); } @Override public void updataCurrentView() { PhoneInfo phoneInfo = mainViewPresenter.getPhoneInfo(); mainBinding.tvPhone.setText("手机号:"+phoneInfo.getTelString()); mainBinding.tvProvince.setText("省份:"+phoneInfo.getProvince()); mainBinding.tvSeller.setText("运营商:"+phoneInfo.getCatName()); mainBinding.tvSeller2.setText("归属运营商:"+phoneInfo.getCarrier()); }}
最后是通用的http请求类
/** * 定义通用http请求网络类 * Created by Administrator on 2017/9/2. */public class HttpUtil { private final OkHttpClient client = new OkHttpClient(); private String url; private Map<String, String> params; private Handler handler = new Handler(Looper.getMainLooper()); public void setHttpResponse(HttpResponse httpResponse) { this.httpResponse = httpResponse; } //设置接口将结果传送回去 private HttpResponse httpResponse; public interface HttpResponse { //失败请求回调 void onFailed(String failure); //成功请求回调 void onSuccess(String result); } /** * Post请求 */ public void sendHttpPost(Map<String, String> param, String netUrl) { doHttpNet(param, netUrl, true); } /** * Get请求 */ public void sendHttpGet(Map<String, String> param, String netUrl) { doHttpNet(param, netUrl, false); } private void doHttpNet(Map<String, String> param, String netUrl, boolean isPost) { url = netUrl; params = param; runHttp(isPost); } private void runHttp(boolean isPost) { Request request = creatRequest(isPost); //请求头弄完在 直接发送请求 client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { if (httpResponse != null) { //回调是在子线程,而UI是在主线程 handler.post(new Runnable() { @Override public void run() { httpResponse.onFailed(e.toString()); } }); } } @Override public void onResponse(Call call, final Response response) throws IOException { if (httpResponse != null) { final String result = response.body().string(); handler.post(new Runnable() { @Override public void run() { if (response.isSuccessful()) { //请求成功 httpResponse.onSuccess(result); }else{ httpResponse.onFailed(result); } } }); } } }); } /** * 创建requst */ private Request creatRequest(boolean isPost) { Request request; if (isPost) { //Post请求 MultipartBody.Builder multipartBody = new MultipartBody.Builder(); multipartBody.setType(MultipartBody.FORM);//表单形式提交请求 //遍历map获取参数 Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> next = iterator.next(); multipartBody.addFormDataPart(next.getKey(), next.getValue()); } //创建请求 request = new Request.Builder().url(url).post(multipartBody.build()).build(); } else { //Get请求,只是拼接参数和网址 Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); StringBuilder strBuilder = new StringBuilder(url + "?");//这里先加进去 while (iterator.hasNext()) { Map.Entry<String, String> next = iterator.next(); strBuilder.append(next.getKey() + "=" + next.getValue() + "&"); } //最后祛除对于的一个“&”符号 String str = strBuilder.toString().substring(0, strBuilder.length() - 1); request = new Request.Builder().url(str).build(); System.out.println("==test==" + str); } return request; }}
阅读全文
0 0
- MVP的实现,后续更新mvp和databinding结合
- Dagger2和MVP的结合
- Android mvp的简单使用和实现
- MVP的实现
- MVP封装成基类结合retrofit和Rxjava
- MVP封装成基类结合retrofit和Rxjava
- 对MVP、MVVM、DataBinding、ButterKnife、Dagger2的初步学习
- Rxandroid与MVP的结合(demo)
- MVP结合OKhttpclient的登录操作
- MVP实现的步骤梳理
- 实现Android的MVP框架
- MVP架构的简单实现
- MVP简单的实现登录
- MVP
- MVP
- MVP
- MVP
- MVP
- 《ThinkPHP5.0+小程序商城构建全栈应用》视频教程下载
- Hyperledger Fabric译文之Membership Service Providers (MSP)
- 硬件系统工程师宝典-目录
- android 按下返回键 一段时间内两次点击结束程序
- HDOJ HDU 1010 Tempter of the Bone
- MVP的实现,后续更新mvp和databinding结合
- ubuntu安装mysql
- H5前端性能测试点及优化方法
- windows下c++如何读取主机名
- HDU 2665 / POJ 2104 静态区间第k小 主席树
- 【学习笔记】你不得不知道的跨域小知识
- Centos5和6 启动流程和故障恢复
- C++拷贝构造函数详解
- Wordpress本地搭建和安装