//主页登陆
package com.bawei.com.dengluadd.IVew;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bawei.com.dengluadd.R;
import com.bawei.com.dengluadd.model.ImodelImp;
import com.bawei.com.dengluadd.presenter.Ipresenterimp;
public class MainActivity extends AppCompatActivity implements Iview,View.OnClickListener{
private EditText mobile;
private EditText password;
private Button login;
private Button reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
mobile = (EditText) findViewById(R.id.tell);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
reg = (Button) findViewById(R.id.zhuce);
login.setOnClickListener(this);
reg.setOnClickListener(this);
}
@Override
public String getmobile() {
return mobile.getText().toString();
}
@Override
public String getpassword() {
return password.getText().toString();
}
@Override
public void jump() {
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
@Override
public void showError() {
Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
Ipresenterimp ipresenterimp=new Ipresenterimp();
ipresenterimp.IPresenterLogin(new ImodelImp() ,this);
break;
case R.id.zhuce:
startActivity(new Intent(MainActivity.this,Main3Activity.class));
break;
}
}
}
//视图
package com.bawei.com.dengluadd.IVew;
/**
* Created by lenovo on 2018/1/9.
*/
public interface Iview {
String getmobile();
String getpassword();
void jump();
void showError();
}
//注册页面
package com.bawei.com.dengluadd.IVew;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bawei.com.dengluadd.R;
import com.bawei.com.dengluadd.model.ImodelImp;
import com.bawei.com.dengluadd.presenter.Ipresenterimp;
public class Main3Activity extends AppCompatActivity implements Iview,View.OnClickListener{
private EditText mobile;
private EditText password;
private Button login;
private Button reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initViews();
}
private void initViews() {
mobile = (EditText) findViewById(R.id.tell);
password = (EditText) findViewById(R.id.password);
reg = (Button) findViewById(R.id.zhuce);
reg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Ipresenterimp i=new Ipresenterimp();
i.IPresenterreg(new ImodelImp(),this);
}
@Override
public String getmobile() {
return mobile.getText().toString();
}
@Override
public String getpassword() {
return password.getText().toString();
}
@Override
public void jump() {
startActivity(new Intent(Main3Activity.this,MainActivity.class));
}
@Override
public void showError() {
Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
//javabean包
package com.bawei.com.dengluadd.model;
/**
* Created by lenovo on 2018/1/9.
*/
public class User {
private String mobile;
private String password;
public User(String mobile, String password) {
this.mobile = mobile;
this.password = password;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"mobile='" + mobile + '\'' +
", password='" + password + '\'' +
'}';
}
}
//IModel接口
package com.bawei.com.dengluadd.model;
import java.util.List;
/**
* Created by lenovo on 2018/1/9.
*/
public interface IModel {
void loginSuccess(User user,LoginListener loginListener);
void regSuccess(User user,RegLoginListenter reginListener);
}
//IModel实现类
package com.bawei.com.dengluadd.model;
import android.util.Log;
import android.widget.Toast;
import com.bawei.com.dengluadd.OkHttpUlis.HttpConig;
import com.bawei.com.dengluadd.OkHttpUlis.okHttpjiexi;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
/**
* Created by lenovo on 2018/1/9.
*/
public class ImodelImp implements IModel {
@Override
public void loginSuccess(User user, final LoginListener loginListener) {
okHttpjiexi ok = new okHttpjiexi();
ok.get(HttpConig.login_url + "?mobile=" + user.getMobile() + "&password=" + user.getPassword());
ok.setLoginlistener(new Loadlisterten() {
@Override
public void LoadSuccess(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String code = jsonObject.getString("code");
if (code.equals("0")) {
loginListener.getloginSuccess(json);
} else {
loginListener.getloginError("登陆失败");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void LoadrError(String json) {
loginListener.getloginSuccess(json);
}
});
}
private static final String TAG = "ImodelImp";
@Override
public void regSuccess(User user, final RegLoginListenter reginListener) {
okHttpjiexi ok = new okHttpjiexi();
ok.get(HttpConig.reg_url + "?mobile=" + user.getMobile() + "&password=" + user.getPassword());
ok.setLoginlistener(new Loadlisterten() {
@Override
public void LoadSuccess(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String code = jsonObject.getString("code");
if (code.equals("0")) {
Log.d(TAG, "注册成功");
reginListener.getregSuccess(json);
} else {
reginListener.getlregError("注册失败");
Log.d(TAG, "注册失败");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void LoadrError(String json) {
Log.d(TAG, "注册失败");
reginListener.getlregError(json);
}
});
}
}
//网络判断接口
package com.bawei.com.dengluadd.model;
/**
* Created by lenovo on 2018/1/9.
*/
public interface Loadlisterten {
void LoadSuccess(String json);
void LoadrError(String json);
}
//登陆接口
package com.bawei.com.dengluadd.model;
/**
* Created by lenovo on 2018/1/9.
*/
public interface LoginListener {
void getloginSuccess(String json);
void getloginError(String json);
}
//注册接口
package com.bawei.com.dengluadd.model;
/**
* Created by lenovo on 2018/1/9.
*/
public interface RegLoginListenter {
void getregSuccess(String json);
void getlregError(String json);
}
////放okhttp解析
package com.bawei.com.dengluadd.OkHttpUlis;
/**
* Created by lenovo on 2018/1/8.
*/
public class HttpConig {
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 getJokes_url="https://www.zhaoapi.cn/quarter/getJokes";
}
import com.bawei.com.dengluadd.model.Loadlisterten;
import com.bawei.com.dengluadd.model.LoginListener;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by lenovo on 2018/1/9.
*/
public class okHttpjiexi {
private MyHandler my = new MyHandler();
private Loadlisterten loadListener;
public void get(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 = my.obtainMessage();
message.what = 0;
message.obj = "失败";
my.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = my.obtainMessage();
String json = response.body().string();
message.what = 1;
message.obj = json;
my.sendMessage(message);
}
});
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if (what == 0) {
String error = (String) msg.obj;
loadListener.LoadrError(error);
}
if (what == 1) {
String json = (String) msg.obj;
loadListener.LoadSuccess(json);
}
}
}
public void setLoginlistener(Loadlisterten loadListener){
this.loadListener=loadListener;
}
}
import com.bawei.com.dengluadd.model.Loadlisterten;
import com.bawei.com.dengluadd.model.LoginListener;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by lenovo on 2018/1/9.
*/
public class okHttpjiexi {
private MyHandler my = new MyHandler();
private Loadlisterten loadListener;
public void get(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 = my.obtainMessage();
message.what = 0;
message.obj = "失败";
my.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message message = my.obtainMessage();
String json = response.body().string();
message.what = 1;
message.obj = json;
my.sendMessage(message);
}
});
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if (what == 0) {
String error = (String) msg.obj;
loadListener.LoadrError(error);
}
if (what == 1) {
String json = (String) msg.obj;
loadListener.LoadSuccess(json);
}
}
}
public void setLoginlistener(Loadlisterten loadListener){
this.loadListener=loadListener;
}
}
//Ipresenter接口
package com.bawei.com.dengluadd.presenter;
import android.content.Context;
import com.bawei.com.dengluadd.IVew.GoodsView;
import com.bawei.com.dengluadd.IVew.Iview;
import com.bawei.com.dengluadd.model.GoodModel;
import com.bawei.com.dengluadd.model.IModel;
/**
* Created by lenovo on 2018/1/9.
*/
public interface Ipresenter {
void IPresenterLogin(IModel iModel, Iview vew);
void IPresenterreg(IModel iModel,Iview vew);
void showGood(Context context, GoodModel goodModel, GoodsView goodsView);
}
//Ipresenter实现类
package com.bawei.com.dengluadd.presenter;
import android.content.Context;
import android.util.Log;
import com.bawei.com.dengluadd.IVew.GoodsView;
import com.bawei.com.dengluadd.IVew.Iview;
import com.bawei.com.dengluadd.IVew.Main2Activity;
import com.bawei.com.dengluadd.OkHttpUlis.HttpConig;
import com.bawei.com.dengluadd.model.GoodModeImp;
import com.bawei.com.dengluadd.model.GoodModel;
import com.bawei.com.dengluadd.model.GoodUser;
import com.bawei.com.dengluadd.model.GoodsLoginlisterten;
import com.bawei.com.dengluadd.model.IModel;
import com.bawei.com.dengluadd.model.LoginListener;
import com.bawei.com.dengluadd.model.RegLoginListenter;
import com.bawei.com.dengluadd.model.User;
import com.google.gson.Gson;
import java.util.List;
/**
* Created by lenovo on 2018/1/9.
*/
public class Ipresenterimp implements Ipresenter{
@Override
public void IPresenterLogin(IModel iModel, final Iview vew) {
iModel.loginSuccess(new User(vew.getmobile(), vew.getpassword()), new LoginListener() {
@Override
public void getloginSuccess(String json) {
vew.jump();
}
@Override
public void getloginError(String json) {
vew.showError();
}
});
}
@Override
public void IPresenterreg(IModel iModel, final Iview vew) {
iModel.regSuccess(new User(vew.getmobile(), vew.getpassword()), new RegLoginListenter() {
@Override
public void getregSuccess(String json) {
vew.jump();
}
@Override
public void getlregError(String json) {
vew.showError();
}
});
}
@Override
public void showGood(final Context c, GoodModel goodModel, final GoodsView goodsView) {
goodModel.GoodSuccess(HttpConig.getJokes_url + "?source=android&appVersion=100&page=1", new GoodsLoginlisterten() {
@Override
public void GoodsLoginSuccess(String json) {
Log.e("tag","有数据++++++++++++++++++++");
Gson g=new Gson();
GoodUser goodUser = g.fromJson(json, GoodUser.class);
List<GoodUser.DataBean> data = goodUser.getData();
goodsView.showdate(c,data);
}
@Override
public void GoodsLoginError(String jsons) {
}
});
}
}
//登陆
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bawei.com.dengluadd.IVew.MainActivity">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:textSize="30dp"/>
<View
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="0.75dp"/>
<EditText
android:id="@+id/tell"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/>
<Button
android:id="@+id/zhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
</LinearLayout>
//注册
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bawei.com.dengluadd.IVew.Main3Activity">
<TextView
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="30dp"/>
<View
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="0.75dp"/>
<EditText
android:id="@+id/tell"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/zhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
</LinearLayout>