//这是抽象类
public interface ContartIntface {
//view login
public interface LoginViewIntface{
//注册的刷新方法
public void showReg(String str);
//登陆的刷新方法
public void showLog(String str);
}
public interface ProductIntface{
//展示列表
public void showProuduct(Object o);
//展示搜索内容
public void showSechProduct(Object o);
}
//p层的接口对象
public interface PersentIntface{
public void pLogin(String phone , String pwd);
public void pRegist(String phone , String pwd);
public void pToRequest(int page);
public void setContPrames(String cont,int page);
}
}
//这个是Model的数据
public class MyModel {
MyCallback myCallback;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int type = msg.arg1;
if(type == 1){
String json = (String) msg.obj;
Gson gson = new Gson();
Bean bean = gson.fromJson(json , Bean.class);
myCallback.success(bean);
}else {
String josn = (String) msg.obj;
try {
JSONObject object = new JSONObject(josn);
String m = object.getString("message");
myCallback.success(m);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
public void postRequset(String url , String phone , String pwd){
OkhttpUtile utile = OkhttpUtile.getInstance();
utile.doPost(url, phone, pwd, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.obj = json;
handler.sendMessage(message);
}
});
}
public void getRequset(String url , Map<String , String> map){
OkhttpUtile utile = OkhttpUtile.getInstance();
utile.doGet(url, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.obj = json;
message.arg1 = 1;
handler.sendMessage(message);
}
});
}
public void setMyCallback(MyCallback back){
myCallback = back;
}
public interface MyCallback{
public void success(Object o);
public void error(String str);
}
}
//这个是P层的数据
public class Mypersenter implements ContartIntface.PersentIntface {
//生命m层的对象
MyModel myModel;
T tt;
public Mypersenter(T t){
myModel = new MyModel();
tt = t;
}
@Override
public void pLogin(String phone, String pwd) {
//先实例化接口
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
ContartIntface.LoginViewIntface loginViewIntface = (ContartIntface.LoginViewIntface) tt;
loginViewIntface.showLog((String) o);
}
@Override
public void error(String str) {
}
});
//开始去请求
myModel.postRequset("http://172.17.8.100/small/user/v1/login" , phone , pwd);
}
@Override
public void pRegist(String phone, String pwd) {
//先实例化接口
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
ContartIntface.LoginViewIntface loginViewIntface = (ContartIntface.LoginViewIntface) tt;
loginViewIntface.showReg((String) o);
}
@Override
public void error(String str) {
}
});
//开始去请求
myModel.postRequset("http://172.17.8.100/small/user/v1/register" , phone , pwd);
}
@Override
public void pToRequest(int page ) {
String url = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
Bean bean = (Bean) o;
List<Bean.ResultBean> list = bean.getResult();
ContartIntface.ProductIntface productIntface = (ContartIntface.ProductIntface) tt;
productIntface.showProuduct(list);
}
@Override
public void error(String str) {
}
});
Map<String , String > map = new HashMap<>();
map.put("keyword" , "高");
map.put("page" , page + "");
map.put("count" , "10");
myModel.getRequset(url , map);
}
@Override
public void setContPrames(String cont , int page) {
String url = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
Map<String , String > map = new HashMap<>();
map.put("keyword" , cont);
map.put("page" , page + "");
map.put("count" , "10");
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
Bean bean = (Bean) o;
List<Bean.ResultBean> list = bean.getResult();
ContartIntface.ProductIntface productIntface = (ContartIntface.ProductIntface) tt;
productIntface.showSechProduct(list);
}
@Override
public void error(String str) {
}
});
myModel.getRequset(url , map);
}
}
//这个Utild
public class OkhttpUtile {
public static OkhttpUtile utile;
OkHttpClient okHttpClient;
//构造方法私有化
private OkhttpUtile(){
okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e(“tag” ,"url = " + request.url());
Response response = chain.proceed(request);
return response;
}
}).build();
}
public static synchronized OkhttpUtile getInstance(){
if(utile == null){
utile = new OkhttpUtile();
}
return utile;
}
//post
public void doPost(String url , String phone , String pwd , Callback callback){
//FormBody.Builde用来封装参数
FormBody.Builder builder = new FormBody.Builder();
builder.add("phone" , phone);
builder.add("pwd" ,pwd);
//通过FormBody.Builder获取到RequestBody对象
RequestBody body = builder.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
public void doGet(String url , Map<String , String> map , Callback callback){
if(map != null && map.size() > 0){
StringBuilder stringBuilder = new StringBuilder();
for(String key : map.keySet()){
String value = map.get(key);
stringBuilder.append(key)
.append("=")
.append(value)
.append("&");
}
String pstr = stringBuilder.toString();
int index = pstr.lastIndexOf("&");
pstr = pstr.substring(0 , index);
url = url+ "?" + pstr;
}
Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}
//这个登录的
public class LoginActivity extends AppCompatActivity implements View.OnClickListener,ContartIntface.LoginViewIntface{
EditText phone ;
EditText pwd;
Button logBut;
Button regBut;
Button qq;
//生命p层的接口对象
ContartIntface.PersentIntface persentIntface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
persentIntface = new Mypersenter(this);
init();
}
public void init(){
phone = findViewById(R.id.phone_id);
pwd = findViewById(R.id.pwd_id);
logBut = findViewById(R.id.login_id);
regBut = findViewById(R.id.regist_id);
qq = findViewById(R.id.qq_id);
qq.setOnClickListener(this);
logBut.setOnClickListener(this);
regBut.setOnClickListener(this);
if(Build.VERSION.SDK_INT>=23){
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(this,mPermissionList,123);
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login_id:
String phoness = phone.getText().toString();
String pwdss = pwd.getText().toString();
persentIntface.pLogin(phoness , pwdss);
break;
case R.id.regist_id:
String phones = phone.getText().toString();
String pwds = pwd.getText().toString();
persentIntface.pRegist(phones , pwds);
break;
case R.id.qq_id:
UMShareAPI umShareAPI = UMShareAPI.get(this);
umShareAPI.getPlatformInfo(this, SHARE_MEDIA.QQ, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
//开始登陆
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//登陆成功
Toast.makeText(LoginActivity.this , "qq登陆成功" , Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this , ProductActivity.class);
startActivity(intent);
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
}
});
break;
}
}
@Override
public void showReg(String str) {
Toast.makeText(this , str , Toast.LENGTH_LONG).show();
}
@Override
public void showLog(String str) {
Toast.makeText(this , str , Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this , ProductActivity.class);
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
}
}
//这个展示的
public class ProductActivity extends AppCompatActivity implements ContartIntface.ProductIntface ,XRecyclerView.LoadingListener {
XRecyclerView xRecyclerView ;
ProductAdapter productAdapter;
List<Bean.ResultBean> mList = new ArrayList<>();
//p
ContartIntface.PersentIntface persentIntface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
xRecyclerView = findViewById(R.id.XRecyclerView_id);
xRecyclerView.setLoadingListener(this);
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setPullRefreshEnabled(true);
GridLayoutManager layoutManager = new GridLayoutManager(this ,2);
xRecyclerView.setLayoutManager(layoutManager);
productAdapter = new ProductAdapter(this , mList);
xRecyclerView.setAdapter(productAdapter);
//去请求数据
persentIntface = new Mypersenter(this);
persentIntface.pToRequest(1 );
}
@Override
public void showProuduct(Object o) {
xRecyclerView.refreshComplete();
xRecyclerView.loadMoreComplete();
List<Bean.ResultBean> list = (List<Bean.ResultBean>) o;
mList.addAll(list);
productAdapter.notifyDataSetChanged();
}
@Override
public void showSechProduct(Object o) {
xRecyclerView.refreshComplete();
xRecyclerView.loadMoreComplete();
mList.clear();
List<Bean.ResultBean> list = (List<Bean.ResultBean>) o;
mList.addAll(list);
productAdapter.notifyDataSetChanged();
}
@Override
public void onRefresh() {
mList.clear();
persentIntface.pToRequest(1 );
}
int page = 1;
@Override
public void onLoadMore() {
page += 1;
persentIntface.pToRequest(page);
}
}
本文介绍了一个基于MVP(Model-View-Presenter)架构的应用实现案例,包括登录模块与商品展示模块的具体设计与实现过程。
3594

被折叠的 条评论
为什么被折叠?



