登录注册搜索(封装mvp)
来源:互联网 发布:软装搭配软件 编辑:程序博客网 时间:2024/06/05 21:17
Model
LoginModel
public class LoginModel { private IPresenterLogin iPresenterLogin; public LoginModel(IPresenterLogin iPresenterLogin) { this.iPresenterLogin = iPresenterLogin; } public void getData(String loginUrl, String umobile, String upwd) { Map<String, String> map = new HashMap<>(); map.put("mobile",umobile); map.put("password",upwd); OkHttp3Util.doPost(loginUrl, map, new Callback() { @Override public void onFailure(Call call, IOException e) { iPresenterLogin.onFailed(e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ String json = response.body().string(); if(json!=null){ //使用原生解析 try { JSONObject obj = new JSONObject(json); //使用接口回调 iPresenterLogin.onSuccess(obj); } catch (Exception e) { e.printStackTrace(); } } } } }); }}
RegModel
public class RegistModel { private IPresenterRegist iPresenterRegist; public RegistModel(RegistPresenter iPresenterRegist) { this.iPresenterRegist = iPresenterRegist; } public void getData(String loginUrl, String umobile, String upwd) { Map<String, String> map = new HashMap<>(); map.put("mobile",umobile); map.put("password",upwd); OkHttp3Util.doPost(loginUrl, map, new Callback() { @Override public void onFailure(Call call, IOException e) { iPresenterRegist.onFailed(e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ String json = response.body().string(); if(json!=null){ //使用原生解析 try { JSONObject obj = new JSONObject(json); //使用接口回调 iPresenterRegist.onSuccess(obj); } catch (Exception e) { e.printStackTrace(); } } } } }); }}
SearchModel
public class SearchModel { private IPresenterSearch iPresenterSearch; public SearchModel(IPresenterSearch iPresenterSearch) { this.iPresenterSearch = iPresenterSearch; } public SearchModel(SearchPresenter searchPresenter) { } public void getData(String loginUrl, String keywords) { Map<String, String> map = new HashMap<>(); Log.d("++++++++++++++",loginUrl); map.put("keywords",keywords); map.put("page","1"); map.put("source","android"); OkHttp3Util.doGet(loginUrl+"?keywords="+keywords+"&page="+1+"&source="+"android", new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ String json = response.body().string(); if(json!=null){ //使用原生解析 try { Gson gson = new Gson(); AllContent allContent = gson.fromJson(json, AllContent.class); //使用接口回调 iPresenterSearch.onSuccess(allContent); } catch (Exception e) { e.printStackTrace(); } } } } }); }}
Preserten
IPresenterLogin
public interface IPresenterLogin { void onSuccess(JSONObject obj); void onFailed(String error);}
IPresenterRegist
public interface IPresenterRegist { void onSuccess(JSONObject obj); void onFailed(String error);}
IPresenterSearch
public interface IPresenterSearch { void onSuccess(AllContent allContent);}
LoginPresenter
public class LoginPresenter implements IPresenterLogin{ private IViewLogin iViewLogin; private final LoginModel regModel; public LoginPresenter(MainActivity iViewLogin) { this.iViewLogin = (IViewLogin) iViewLogin; regModel = new LoginModel(this); } public void getData(String loginUrl, String umobile, String upwd) { regModel.getData(loginUrl,umobile,upwd); } @Override public void onSuccess(JSONObject obj) { iViewLogin.onSuccess(obj); } @Override public void onFailed(String error) { iViewLogin.onFailed(error); }}
RegistPresenter
public class RegistPresenter implements IPresenterRegist{ private IViewRegist iViewRegist; private final RegistModel registModel; public RegistPresenter(RegActivity iViewRegist) { this.iViewRegist = iViewRegist; registModel = new RegistModel(this); } public void getData(String loginUrl, String umobile, String upwd) { registModel.getData(loginUrl,umobile,upwd); } @Override public void onSuccess(JSONObject obj) { iViewRegist.onSuccess(obj); } @Override public void onFailed(String error) { iViewRegist.onFailed(error); }}
SearchPresenter
public abstract class SearchPresenter implements IPresenterSearch{ SearchModel searchModel = new SearchModel(this); private IViewSearch iViewSearch; public SearchPresenter(SearchActivity iViewSearch) { this.iViewSearch = (IViewSearch) iViewSearch; } public void getData(String searchUrl, String keywords) { searchModel.getData(searchUrl,keywords); } public void onSuccess(AllContent allContent) { iViewSearch.onSuccess(allContent); }}
ApiUtil
public class ApiUtil { public static final String LOGIN_URL = "https://www.zhaoapi.cn/user/login"; public static final String REGIST_URL = "https://www.zhaoapi.cn/user/reg"; public static final String SEARCH_URL = "http://120.27.23.105/product/searchProducts";}
Activity
LoginActivity
public class MainActivity extends AppCompatActivity { private TextView login_umobile; private TextView login_upwd; private JSONObject data; private LoginPresenter regPresenter; private String umobile; private String upwd; private SharedPreferences preferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); login_umobile = (TextView) findViewById(R.id.login_umobile); login_upwd = (TextView) findViewById(R.id.login_upwd); //创建中间者 regPresenter = new LoginPresenter(this); //存储数据的对象 preferences = getSharedPreferences("logindata", MODE_PRIVATE); boolean islogin = preferences.getBoolean("islogin", false); if(islogin){ Intent intent = new Intent(MainActivity.this,SearchActivity.class); startActivity(intent); } } public void regist(View view) { Intent intent = new Intent(MainActivity.this,RegActivity.class); startActivity(intent); } //登录 public void login(View view) { umobile = login_umobile.getText().toString(); upwd = login_upwd.getText().toString(); regPresenter.getData(ApiUtil.LOGIN_URL, umobile, upwd); } public void onSuccess(final JSONObject obj){ runOnUiThread(new Runnable() { @Override public void run() { try { String code = obj.getString("code"); if("0".equals(code)){ //登录成功后,将数据存入SharedPreferences SharedPreferences.Editor edit = preferences.edit(); edit.putString("mobile",umobile); edit.putString("password",upwd); edit.putBoolean("islogin",true); edit.commit(); Intent intent = new Intent(MainActivity.this,SearchActivity.class); startActivity(intent); Toast.makeText(MainActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); } public void onFailed(String error) { Log.d("MainActivity请求失败原因",error); }}
RegActivity
public class RegActivity extends AppCompatActivity implements IViewRegist { private TextView reg_upwd; private TextView reg_umobile; private String umobile; private String upwd; private RegistPresenter registPresenter; private TextView fan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reg); reg_umobile = (TextView) findViewById(R.id.reg_umobile); reg_upwd = (TextView) findViewById(R.id.reg_upwd); fan = (TextView) findViewById(R.id.fan); registPresenter = new RegistPresenter(this); //点击返回 fan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } //立即注册 public void goRegist(View view) { umobile = reg_umobile.getText().toString(); upwd = reg_upwd.getText().toString(); registPresenter.getData(ApiUtil.REGIST_URL,umobile,upwd); } @Override public void onSuccess(final JSONObject obj) { runOnUiThread(new Runnable() { @Override public void run() { try { String code = obj.getString("code"); if("0".equals(code)){ Toast.makeText(RegActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(RegActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); } @Override public void onFailed(String error) { Log.d("RegActivity请求失败原因",error); }}
SearchActivity
public class SearchActivity extends AppCompatActivity implements IViewSearch{ private EditText shu; private RecyclerView recycler_view; private SearchPresenter searchPresenter; private CheckBox ck; boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); shu = (EditText) findViewById(R.id.shu); recycler_view = (RecyclerView) findViewById(R.id.recycler_view); ck = (CheckBox) findViewById(R.id.ck); searchPresenter = new SearchPresenter(this); } //搜索 public void search(View view) { try { String name = shu.getText().toString(); String keywords = URLEncoder.encode(name, "utf-8"); searchPresenter.getData(ApiUtil.SEARCH_URL,keywords); } catch (Exception e) { e.printStackTrace(); } } //切换布局(网格---列表) @Override public void onSuccess(final AllContent allContent) { runOnUiThread(new Runnable() { @Override public void run() { final List<AllContent.DataBean> data = allContent.getData(); Toast.makeText(SearchActivity.this, allContent.getMsg(),Toast.LENGTH_SHORT).show(); //设置适配器 /*SearchRecyclerAdapter adapter = new SearchRecyclerAdapter(SearchActivity.this,data); recycler_view.setAdapter(adapter); recycler_view.setLayoutManager(new LinearLayoutManager(SearchActivity.this, LinearLayout.VERTICAL,false));*/ setAdapter(data); ck.setChecked(flag); ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(flag){ //设置管理器、适配器 setAdapter(data); ck.setChecked(false); flag = ck.isChecked(); }else{ SearchRecyclerGVAdapter gvAdapter = new SearchRecyclerGVAdapter(SearchActivity.this, data); recycler_view.setAdapter(gvAdapter); recycler_view.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)); ck.setChecked(true); flag = ck.isChecked(); } } }); } }); } private void setAdapter(List<AllContent.DataBean> data) { SearchRecyclerAdapter adapter = new SearchRecyclerAdapter(SearchActivity.this,data); recycler_view.setAdapter(adapter); recycler_view.setLayoutManager(new LinearLayoutManager(SearchActivity.this, LinearLayout.VERTICAL,false)); }}
SearchRecyclerHolder
public class SearchRecyclerHolder extends RecyclerView.ViewHolder { public ImageView img; public TextView title; public TextView price; public SearchRecyclerHolder(View itemView) { super(itemView); img = (ImageView) itemView.findViewById(R.id.img); price = (TextView) itemView.findViewById(R.id.price); title = (TextView) itemView.findViewById(R.id.title); }}
Interfac
IViewLogin
public interface IViewLogin { void onSuccess(JSONObject obj); void onFailed(String error);}
IViewRegist
public interface IViewRegist { void onSuccess(JSONObject obj); void onFailed(String error);}
IViewSearch
public interface IViewSearch { void onSuccess(AllContent allContent);}
Adapter
SearchRecyclerAdapter
public class SearchRecyclerAdapter extends RecyclerView.Adapter<SearchRecyclerHolder> { Context context; List<AllContent.DataBean> data; public SearchRecyclerAdapter(Context context, List<AllContent.DataBean> data) { this.context = context; this.data = data; } @Override public SearchRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); SearchRecyclerHolder searchRecyclerHolder = new SearchRecyclerHolder(view); return searchRecyclerHolder; } @Override public void onBindViewHolder(SearchRecyclerHolder holder, int position) { AllContent.DataBean dataBean = data.get(position); String images = dataBean.getImages(); String[] split = images.split("\\|"); //设置数据 Glide.with(context).load(split[0]).into(holder.img); holder.price.setText(dataBean.getPrice()+""); holder.title.setText(dataBean.getTitle()); } @Override public int getItemCount() { return data.size(); }}
SearchRecyclerGVAdapter
public class SearchRecyclerGVAdapter extends RecyclerView.Adapter<SearchRecyclerHolder> { Context context; List<AllContent.DataBean> data; public SearchRecyclerGVAdapter(Context context, List<AllContent.DataBean> data) { this.context = context; this.data = data; } @Override public SearchRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false); SearchRecyclerHolder searchRecyclerHolder = new SearchRecyclerHolder(view); return searchRecyclerHolder; } @Override public void onBindViewHolder(SearchRecyclerHolder holder, int position) { AllContent.DataBean dataBean = data.get(position); String images = dataBean.getImages(); String[] split = images.split("\\|"); //设置数据 Glide.with(context).load(split[0]).into(holder.img); holder.price.setText(dataBean.getPrice()+""); holder.title.setText(dataBean.getTitle()); } @Override public int getItemCount() { return data.size(); }}
布局文件
Login
<TextView android:layout_width="match_parent" android:layout_height="60dp" android:text="登录" android:textSize="35dp" android:gravity="center"/> <TextView android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="5dp"/> <EditText android:id="@+id/login_umobile" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_gravity="center" android:hint="请输入手机号" android:layout_marginTop="50dp" android:layout_marginBottom="30dp"/> <EditText android:id="@+id/login_upwd" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_gravity="center" android:hint="请输入密码" android:layout_marginBottom="30dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="30dp"> <Button android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="登录" android:onClick="login"/> <Button android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="注册" android:onClick="regist"/> </LinearLayout>
Reg
<RelativeLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:id="@+id/fan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="<" android:textSize="35dp"/> <TextView android:layout_width="match_parent" android:layout_height="60dp" android:text="登录" android:textSize="35dp" android:gravity="center"/> </RelativeLayout> <TextView android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="5dp"/> <EditText android:id="@+id/reg_umobile" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_gravity="center" android:hint="请输入手机号" android:layout_marginTop="50dp" android:layout_marginBottom="30dp"/> <EditText android:id="@+id/reg_upwd" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_gravity="center" android:hint="请输入密码" android:layout_marginBottom="30dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="立即注册" android:textSize="25dp" android:textColor="#89f" android:onClick="goRegist" android:layout_gravity="center"/>
Search
<RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索商品" android:textSize="35dp" android:layout_centerInParent="true"/> <CheckBox android:button="@null" android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/ck" android:background="@drawable/myselect" android:layout_alignParentRight="true" /> </RelativeLayout> <TextView android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="3dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:orientation="horizontal"> <EditText android:id="@+id/shu" android:layout_width="220dp" android:layout_height="wrap_content" android:hint="请输入关键字" android:layout_marginLeft="50dp"/> <Button android:onClick="search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" android:layout_alignRight="@id/shu"/> </LinearLayout> <TextView android:background="#f1ebeb" android:layout_width="match_parent" android:layout_height="3dp"/> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
搜索切换布局
Grid_item
<ImageView android:id="@+id/img" android:layout_width="180dp" android:layout_height="180dp" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:text="bbbbbbbbb" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:id="@+id/price" android:text="aaaaa" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
list_item
<ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:text="bbbbbbbbb" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:id="@+id/price" android:text="aaaaa" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
添加依赖
compile 'com.squareup.okhttp3:okhttp:3.6.0' compile 'com.squareup.okio:okio:1.11.0' compile 'com.orhanobut:logger:2.1.1' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.android.support:recyclerview-v7:25.3.1' testCompile 'junit:junit:4.12' compile 'com.google.code.gson:gson:2.8.2'
阅读全文
0 0
- 登录注册搜索(封装mvp)
- 封装MVP 实现登录注册功能
- MVP 实现登录注册
- 登录注册mvp模式
- MVP注册登录
- MVP 登录注册
- MVP注册登录
- MVP 登录+注册
- MVP登录注册
- MVP登录注册
- MVP登录注册
- Mvp实现登录注册
- MVP登录注册跳转
- MVP登录注册
- MVP登录注册、跳转
- MVP实现登录注册
- 登录注册mvp
- 登录注册(MVp)
- 【操作系统】IO 缓冲技术
- JAVA程序员怎么样才能进一线互联网公司
- Docker架构
- 浅谈Object Head First Java笔记
- python爬虫——获取新浪新闻前两页新闻信息
- 登录注册搜索(封装mvp)
- sql笔记
- Android_RecyclerView
- Java读取TXT文件的方法
- std 中for_each 算法与c++11范围for语句
- Redis 分布式锁的正确实现方式( Java 版 )
- springmvc 处理的过程详解
- SSH信任关系设置-oracle rac 信任关系
- UVA10870(Recurrents)