//OkHttpUtils
public class OkHttpUtils {
private Handler handler=new Handler();
private OkHttpClient okHttpClient;
private static OkHttpUtils mInstance;
/**
* 私有构造
*/
private OkHttpUtils(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.build();
}
public static OkHttpUtils getmInstance() {
if (mInstance==null){
synchronized (OkHttpUtils.class){
if (mInstance==null){
mInstance=new OkHttpUtils();
}
}
}
return mInstance;
}
public void Post(String url, HashMap<String,String> params,final HttpCallBack httpCallBack){
//请求体和相应
FormBody.Builder formbody = new FormBody.Builder();
for (Map.Entry<String,String> p:params.entrySet()){
formbody.add(p.getKey(),p.getValue());
}
final RequestBody requestBody = formbody.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (httpCallBack!=null){
handler.post(new Runnable() {
@Override
public void run() {
httpCallBack.FailUre("网络异常");
}
});
}
}
@Override
public void onResponse(Call call,final Response response) throws IOException {
if (requestBody!=null){
if (response.code()==200){
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
httpCallBack.Success(result);
}
});
}
}
}
});
}
}
//ValidatorUtil验证
/**
* 正则表达式:验证用户名(不包含中文和特殊字符)如果用户名使用手机号码或邮箱 则结合手机号验证和邮箱验证
*/
public static final String REGEX_USERNAME = “1\w{5,17}$”;
/**
* 正则表达式:验证密码(不包含特殊字符)
*/
public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{1,12}$";
/**
* 正则表达式:验证手机号
*/
public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$";
/**
* 正则表达式:验证邮箱
*/
public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
/**
* 正则表达式:验证汉字(1-9个汉字) {1,9} 自定义区间
*/
public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5]{1,9}$";
/**
* 正则表达式:验证身份证
*/
public static final String REGEX_ID_CARD = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
/**
* 正则表达式:验证URL
*/
public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
/**
* 正则表达式:验证IP地址
*/
public static final String REGEX_IP_ADDR = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
/**
* 校验用户名
*
* @param username
* @return 校验通过返回true,否则返回false
*/
public static boolean isUserName(String username) {
return Pattern.matches(REGEX_USERNAME, username);
}
/**
* 校验密码
*
* @param password
* @return 校验通过返回true,否则返回false
*/
public static boolean isPassword(String password) {
return Pattern.matches(REGEX_PASSWORD, password);
}
/**
* 校验手机号
*
* @param mobile
* @return 校验通过返回true,否则返回false
*/
public static boolean isMobile(String mobile) {
return Pattern.matches(REGEX_MOBILE, mobile);
}
/**
* 校验邮箱
*
* @param email
* @return 校验通过返回true,否则返回false
*/
public static boolean isEmail(String email) {
return Pattern.matches(REGEX_EMAIL, email);
}
/**
* 校验汉字
*
* @param chinese
* @return 校验通过返回true,否则返回false
*/
public static boolean isChinese(String chinese) {
return Pattern.matches(REGEX_CHINESE, chinese);
}
/**
* 校验身份证
*
* @param idCard
* @return 校验通过返回true,否则返回false
*/
public static boolean isIDCard(String idCard) {
return Pattern.matches(REGEX_ID_CARD, idCard);
}
/**
* 校验URL
*
* @param url
* @return 校验通过返回true,否则返回false
*/
public static boolean isUrl(String url) {
return Pattern.matches(REGEX_URL, url);
}
/**
* 校验IP地址
*
* @param ipAddress
* @return
*/
public static boolean isIPAddress(String ipAddress) {
return Pattern.matches(REGEX_IP_ADDR, ipAddress);
}
}
//HttpCallBack接口
public interface HttpCallBack {
void Success(String result);
void FailUre(String msg);
}
//RequestCallBack接口
public interface RequestCallBack {
void Success(String result);
void FailUre(String msg);
}
//ShowContract
public interface ShowContract {
abstract class ShowPresenter{
public abstract void register(HashMap<String,String> params);
public abstract void login(HashMap<String,String> params);
}
interface ShowModel{
void register(HashMap<String,String> params, com.example.dllianxi.model.ShowModel.ModelCallBack modelCallBack);
void login(HashMap<String,String> params,com.example.dllianxi.model.ShowModel.ModelCallBack modelCallBack);
}
interface ShowView{
void Success(String result);
void Fail(String msg);
void MobileError(String error);
}
}
***//apiUrl ***
public class apiUrl {
public static final String USER_REG = "http://120.27.23.105/user/reg?";
public static final String USER_LOGIN = "http://120.27.23.105/user/login?";
}
//ShowPresenter
public class ShowPresenter extends ShowContract.ShowPresenter {
private ShowModel showModel;
private ShowContract.ShowView showView;
public ShowPresenter(ShowContract.ShowView showView){
this.showModel=new ShowModel();
this.showView=showView;
}
@Override
public void register(HashMap<String, String> params) {
String phone = params.get(“mobile”);
if (!ValidatorUtil.isMobile(phone)){
if (showView!=null){
showView.MobileError(“手机号不合法”);
}
return;
}
showModel.register(params, new ShowModel.ModelCallBack() {
@Override
public void success(String result) {
if (showModel!=null){
showView.Success(result);
}
}
@Override
public void failUre(String msg) {
if (showView!=null){
showView.Fail(msg);
}
}
});
}
@Override
public void login(HashMap<String, String> params) {
String phone = params.get("mobile");
if (!ValidatorUtil.isMobile(phone)){
if (showView!=null){
showView.MobileError("手机号错误");
}
return;
}
showModel.login(params, new ShowModel.ModelCallBack() {
@Override
public void success(String result) {
if (showModel!=null){
showView.Success(result);
}
}
@Override
public void failUre(String msg) {
if (showModel!=null){
showView.Fail(msg);
}
}
});
}
}
//ShowModel
public class ShowModel implements ShowContract.ShowModel{
@Override
public void register(HashMap<String, String> params, final ModelCallBack modelCallBack) {
OkHttpUtils.getmInstance().Post(apiUrl.USER_REG, params, new HttpCallBack() {
@Override
public void Success(String result) {
if (modelCallBack!=null){
modelCallBack.success(result);
}
}
@Override
public void FailUre(String msg) {
if (modelCallBack!=null){
modelCallBack.failUre(msg);
}
}
});
}
@Override
public void login(HashMap<String, String> params, final ModelCallBack modelCallBack) {
OkHttpUtils.getmInstance().Post(apiUrl.USER_LOGIN, params, new HttpCallBack() {
@Override
public void Success(String result) {
if (modelCallBack!=null){
modelCallBack.success(result);
}
}
@Override
public void FailUre(String msg) {
if (modelCallBack!=null){
modelCallBack.failUre(msg);
}
}
});
}
public interface ModelCallBack{
void success(String result);
void failUre(String msg);
}
}
//LoginActivity
public class LoginActivity extends AppCompatActivity implements ShowContract.ShowView {
private EditText edPhone;
private EditText edPassword;
private Button btnLogin;
private Button btnReg;
private ShowPresenter showPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
showPresenter = new ShowPresenter(this);
}
public void btn_login(View view) {
//获取数据
String login_phone = edPhone.getText().toString();
String login_pwd = edPassword.getText().toString();
HashMap<String, String> params = new HashMap<>();
params.put("mobile",login_phone);
params.put("password",login_pwd);
Toast.makeText(LoginActivity.this,params+"",Toast.LENGTH_SHORT).show();
if (showPresenter!=null){
showPresenter.login(params);
}
}
public void btn_reg(View view) {
Intent intent = new Intent(LoginActivity.this, RegActivity.class);
startActivity(intent);
}
@Override
public void Success(String result) {
Toast.makeText(LoginActivity.this,result,Toast.LENGTH_SHORT).show();
LoginBean loginBean = new Gson().fromJson(result, LoginBean.class);
Toast.makeText(LoginActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
if(loginBean.getMsg().equals("登录成功")){
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
}
@Override
public void Fail(String msg) {
Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();
}
@Override
public void MobileError(String error) {
Toast.makeText(LoginActivity.this,error,Toast.LENGTH_SHORT).show();
}
private void initView() {
edPhone = (EditText) findViewById(R.id.ed_phone);
edPassword = (EditText) findViewById(R.id.ed_password);
btnLogin = (Button) findViewById(R.id.btn_login);
btnReg = (Button) findViewById(R.id.btn_reg);
}
}
//RegActivity
public class RegActivity extends AppCompatActivity implements ShowContract.ShowView {
private EditText edPhone;
private EditText edPassword;
private Button btnLogin;
private ShowPresenter showPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
initView();
}
private void initView() {
edPhone = (EditText) findViewById(R.id.ed_phone);
edPassword = (EditText) findViewById(R.id.ed_password);
btnLogin = (Button) findViewById(R.id.btn_login);
showPresenter = new ShowPresenter(this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入框内容
String ed_phones = edPhone.getText().toString();
String ed_passwords = edPassword.getText().toString();
HashMap<String, String> params = new HashMap<>();
params.put("mobile",ed_phones);
params.put("password",ed_passwords);
Toast.makeText(RegActivity.this,params+"",Toast.LENGTH_SHORT).show();
if (showPresenter!=null){
showPresenter.register(params);
}
}
});
}
public void btn_login(View view) {
Intent intent = new Intent(RegActivity.this, LoginActivity.class);
startActivity(intent);
}
@Override
public void Success(String result) {
RegBean regBean = new Gson().fromJson(result, RegBean.class);
Toast.makeText(RegActivity.this,regBean.getMsg(),Toast.LENGTH_SHORT).show();
if (regBean.getMsg().equals("注册成功")){
Toast.makeText(RegActivity.this,regBean.getMsg(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void Fail(String msg) {
Toast.makeText(RegActivity.this,msg,Toast.LENGTH_SHORT).show();
}
@Override
public void MobileError(String error) {
Toast.makeText(RegActivity.this,error,Toast.LENGTH_SHORT).show();
}
}
//main布局登录
a-zA-Z ↩︎