/**
* @Author: zhang
* @Date: 2019/3/21 8:45
* @Description:M层
*/
public class MyModel implements MyInterface.ModelInterface {
MyCallBack myCallBack;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String json = (String) msg.obj;
if (msg.arg1 == 1){
try {
JSONObject object = new JSONObject(json);
String m = object.getString("message");
myCallBack.success(m);
} catch (JSONException e) {
e.printStackTrace();
}
}else {
Gson gson = new Gson();
ShopBean bean = gson.fromJson(json,ShopBean.class);
myCallBack.success(bean);
}
}
};
@Override
public void PostRequest(String url,String phone,String pwd,MyCallBack myCallBack) {
this.myCallBack = myCallBack;
OkHttpUtil.getInstance().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;
message.arg1 = 1;
handler.sendMessage(message);
}
});
}
@Override
public void toRequest(String url, MyCallBack myCallBack) {
this.myCallBack = myCallBack;
OkHttpUtil.getInstance().doGet(url,handler);
}
public interface MyCallBack{
public void success(Object obj);
public void error(Object obj);
}
}
/**
* @Author: zhang
* @Date: 2019/3/21 8:48
* @Description:P层
*/
public class MyPresenter<T> implements MyInterface.PresenterInterface {
//声明Model接口对象
MyInterface.ModelInterface modelInterface;
//声明View接口对象
MyInterface.ViewInterface viewInterface;
MyInterface.ViewInterface.LoginInterface loginInterface;
T tt;
String lUrl = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
public MyPresenter(T tt) {
modelInterface = new MyModel();
this.tt = tt;
}
@Override
public void toRegister(String phone,String pwd) {
viewInterface = (MyInterface.ViewInterface) tt;
modelInterface.PostRequest("http://172.17.8.100/small/user/v1/register",phone,pwd, new MyModel.MyCallBack() {
@Override
public void success(Object obj) {
viewInterface.showRegister((String) obj);
}
@Override
public void error(Object obj) {
}
});
}
@Override
public void toLogin(String phone,String pwd) {
viewInterface = (MyInterface.ViewInterface) tt;
modelInterface.PostRequest("http://172.17.8.100/small/user/v1/login",phone,pwd, new MyModel.MyCallBack() {
@Override
public void success(Object obj) {
viewInterface.showLogin((String) obj);
}
@Override
public void error(Object obj) {
}
});
}
@Override
public void toModel(String key,int page) {
Log.i("tag","P"+key);
loginInterface = (MyInterface.ViewInterface.LoginInterface) tt;
String dataUrl = lUrl +"?count=10&page="+page+"&keyword="+key;
modelInterface.toRequest(dataUrl, new MyModel.MyCallBack() {
@Override
public void success(Object obj) {
if (obj instanceof ShopBean){
ShopBean bean = (ShopBean) obj;
loginInterface.reFreDisplay(bean.getResult());
}
}
@Override
public void error(Object obj) {
}
});
}
}
/**
* @Author: zhang
* @Date: 2019/3/21 8:54
* @Description:工具类
*/
public class OkHttpUtil {
OkHttpClient okHttpClient;
public static OkHttpUtil okHttpUtil;
//构造方法私有化
private OkHttpUtil() {
okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
}
public static synchronized OkHttpUtil getInstance(){
if (okHttpUtil == null){
okHttpUtil = new OkHttpUtil();
}
return okHttpUtil;
}
public void doPost(String url, String phone, String pwd, Callback callback){
//FormBody.Builder用来封装参数
FormBody.Builder build = new FormBody.Builder();
build.add("phone",phone);
build.add("pwd",pwd);
RequestBody body = build.build();
Request request = new Request.Builder().url(url).post(body).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
public void doGet(String url, final Handler mHandler){
Request request = new Request.Builder().url(url).get().build();
Call call = okHttpClient.newCall(request);
call.enqueue(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;
mHandler.sendMessage(message);
}
});
}
private class MyInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.i("tag","url = "+request.url());
Response proceed = chain.proceed(request);
return proceed;
}
}
}
V层
public class LoginActivity extends AppCompatActivity implements MyInterface.ViewInterface.LoginInterface {
List<ShopBean.ResultBean> list = new ArrayList<>();
MyInterface.PresenterInterface presenterInterface;
XRecyclerView xRecyclerView;
MyAdapter adapter;
int page = 1;
String key = "高";
MyLineLayout myLineLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
presenterInterface = new MyPresenter(this);
init();
}
private void init() {
xRecyclerView = findViewById(R.id.xRecycler_id);
myLineLayout = findViewById(R.id.myLineLayout);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xRecyclerView.setLayoutManager(layoutManager);
adapter = new MyAdapter(list,this);
xRecyclerView.setAdapter(adapter);
Log.i("tag","V"+key);
presenterInterface.toModel(key,page);
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setPullRefreshEnabled(true);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
list.clear();
page = 1;
presenterInterface.toModel(key,page);
xRecyclerView.refreshComplete();
}
@Override
public void onLoadMore() {
page++;
presenterInterface.toModel(key,page);
xRecyclerView.loadMoreComplete();
}
});
}
@Override
public void reFreDisplay(List<ShopBean.ResultBean> vList) {
list.addAll(vList);
Log.i("tag","个数"+list.size());
adapter.notifyDataSetChanged();
}
}
登录注册
public class ViewActivity extends AppCompatActivity implements MyInterface.ViewInterface, View.OnClickListener {
EditText edit_phone,edit_pwd;
Button but_login,but_register;
ImageView imageView;
MyInterface.PresenterInterface presenterInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
presenterInterface = new MyPresenter(this);
init();
}
private void init() {
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);
}
imageView = findViewById(R.id.qq_id);
edit_phone = findViewById(R.id.phone_id);
edit_pwd = findViewById(R.id.pwd_id);
but_login = findViewById(R.id.but_login_id);
but_register = findViewById(R.id.but_register_id);
but_login.setOnClickListener(this);
but_register.setOnClickListener(this);
imageView.setOnClickListener(this);
}
@Override
public void showRegister(String str) {
Toast.makeText(this,str,Toast.LENGTH_LONG).show();
}
@Override
public void showLogin(String str) {
if (str.equals("登录成功")){
Intent intent = new Intent(ViewActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
//登录
case R.id.but_login_id:
String lPhone = edit_phone.getText().toString();
String lPwd = edit_pwd.getText().toString();
presenterInterface.toLogin(lPhone,lPwd);
break;
//注册
case R.id.but_register_id:
//获取输入框输数据
String rPhone = edit_phone.getText().toString();
String rPwd = edit_pwd.getText().toString();
presenterInterface.toRegister(rPhone,rPwd);
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) {
//开始登录
Toast.makeText(ViewActivity.this,"开始登录",Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//登录成功
Toast.makeText(ViewActivity.this,"登录成功",Toast.LENGTH_LONG).show();
Intent intent = new Intent(ViewActivity.this,LoginActivity.class);
startActivity(intent);
finish();
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
//登录失败
Toast.makeText(ViewActivity.this,"登录失败",Toast.LENGTH_LONG).show();
Log.i("tag","登录失败");
Log.i("tag",""+throwable);
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
}
});
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
}