本文為菜鳥窩作者蔣志碧的連載。“從 0 開始開發(fā)一款直播 APP ”系列來聊聊時下最火的直播 APP墙懂,如何完整的實現(xiàn)一個類"騰訊直播"的商業(yè)化項目
視頻地址:http://www.cniao5.com/course/10121
【從 0 開始開發(fā)一款直播 APP】4.1 網(wǎng)絡(luò)封裝之 Okhttp -- 基礎(chǔ)回顧
【從 0 開始開發(fā)一款直播 APP】4.2 網(wǎng)絡(luò)封裝之 OkHttp -- GET损搬,POST,前后端交互
【從 0 開始開發(fā)一款直播 APP】4.3 網(wǎng)絡(luò)封裝之 OkHttp -- 封裝 GET柜与,POST FORM巧勤,POST JSON
【從 0 開始開發(fā)一款直播 APP】4.4 網(wǎng)絡(luò)封裝之 OkHttp -- 網(wǎng)絡(luò)請求實現(xiàn)直播登錄
上一章講了 OkHttp 的封裝颅悉,現(xiàn)在來使用一下封裝吧迁匠。
網(wǎng)絡(luò)請求進行手機登錄和用戶名登錄。
先來介紹一個請求工具 postman豌鹤,Google 瀏覽器上的一個插件傍药,也有對應(yīng)的應(yīng)用程序魂仍,親測很好用擦酌,推薦給大家赊舶。
好了笼平,開始講解網(wǎng)絡(luò)請求舔痪,測試一下封裝的 Okhttp 框架锄码。
講解之前添加依賴以及相關(guān)權(quán)限
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.zhy:okhttputils:2.6.2'
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
一滋捶、界面布局
里面用到了一個控件重窟,很漂亮,看到輸入框的文字會跳動對吧扭仁,就是這個控件斋枢。android.support.design.widget.TextInputLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:background="@mipmap/bg_dark"
android:id="@+id/rl_login_root"
tools:context="com.dali.admin.livestreaming.activity.LoginActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="100dp">
//驗證碼
<Button
android:id="@+id/btn_verify_code"
android:layout_width="90dp"
android:layout_height="35dp"
android:layout_alignEnd="@+id/til_login"
android:layout_alignRight="@+id/til_login"
android:layout_below="@+id/til_login"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:background="@drawable/btn_login"
android:clickable="true"
android:minWidth="0dp"
android:padding="4dp"
android:text="@string/activity_login_verify_code"
android:textSize="12sp"
style="@style/loginButton"
/>
<android.support.design.widget.TextInputLayout
android:id="@+id/til_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
//用戶名
<AutoCompleteTextView
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:hint="@string/activity_login_username"
android:inputType="textEmailAddress"
android:maxLength="24"
android:maxLines="1"
android:singleLine="true"
android:textColor="@drawable/tv_selector"
android:textColorHint="@color/white"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/til_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/til_login">
//密碼
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et_login"
android:ellipsize="end"
android:hint="@string/activity_login_password"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLength="24"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"/>
</android.support.design.widget.TextInputLayout>
//登錄
<Button
android:id="@+id/btn_login"
android:layout_alignParentEnd="true"
android:layout_below="@+id/til_password"
android:background="@drawable/btn_login"
android:clickable="true"
android:padding="0dp"
android:text="@string/activity_login_btnlogin"
style="@style/loginButton"/>
//手機號登錄
<Button
android:id="@+id/btn_phone_login"
android:layout_width="wrap_content"
android:layout_below="@+id/btn_login"
android:layout_marginTop="10dp"
android:text="@string/activity_login_btnphone"
android:background="?attr/selectableItemBackground"
style="@style/loginButton"
/>
//注冊新用戶
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_alignEnd="@+id/btn_login"
android:layout_alignRight="@+id/btn_login"
android:layout_below="@+id/btn_login"
android:background="?attr/selectableItemBackground"
android:layout_marginTop="10dp"
android:text="@string/activity_login_register"
style="@style/loginButton"
/>
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignTop="@+id/btn_login"
android:layout_centerHorizontal="true"
android:visibility="gone"/>
</RelativeLayout>
</RelativeLayout>
二戈次、登錄界面邏輯實現(xiàn)
2.1、類的初始化
創(chuàng)建 LoginActivity 類绊寻,繼承 BaseActivity澄步,實現(xiàn)其抽象方法村缸。實例化控件武氓。
public class LoginActivity extends BaseActivity implements View.OnClickListener{
//控件
private ProgressBar progressBar;
private EditText etPassword;
private EditText etLogin;
private Button btnLogin;
private Button btnPhoneLogin;
private TextInputLayout tilLogin, tilPassword;
private Button btnRegister;
private TextView tvVerifyCode;
//是否是手機號登錄
private boolean isPhoneLogin = false;
@Override
protected void setActionBar() {
}
@Override
protected void setListener() {
}
@Override
protected void initData() {
}
@Override
protected void initView() {
etLogin = obtainView(R.id.et_username);
etPassword = obtainView(R.id.et_password);
btnRegister = obtainView(R.id.btn_register);
btnPhoneLogin = obtainView(R.id.btn_phone_login);
btnLogin = obtainView(R.id.btn_login);
progressBar = obtainView(R.id.progressbar);
tilLogin = obtainView(til_login);
tilPassword = obtainView(R.id.til_password);
tvVerifyCode = obtainView(R.id.btn_verify_code);
//第一次進入系統(tǒng)默認是用戶名登錄
userNameLoginViewInit();
}
@Override
protected int getLayoutId() {
return R.layout.activity_login;
}
@Override
public void onClick(View v) {
}
}
2.2东羹、用戶名登錄實現(xiàn)
這里實現(xiàn)邏輯忠烛,解釋一下實現(xiàn)思路况木,而登錄請求網(wǎng)絡(luò)后面介紹火惊。
//用戶名登錄方法
public void userNameLoginViewInit() {
//用戶名登錄控件初始化
userLoginTrans();
//注冊監(jiān)聽
btnRegister.setOnClickListener(this);
//這里只是用做切換界面屹耐,用戶名登錄情況下點擊按鈕顯示到手機號登錄,顯示獲取驗證碼按鈕
btnPhoneLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//手機號登錄切換
phoneLoginViewinit();
}
});
//登錄按鈕監(jiān)聽
btnLogin.setOnClickListener(this);
}
//用戶名登錄界面控件顯示
private void userLoginTrans() {
//將手機號登錄設(shè)置為 false
isPhoneLogin = false;
//隱藏獲取驗證碼按鈕
tvVerifyCode.setVisibility(View.GONE);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(250);
tvVerifyCode.setAnimation(alphaAnimation);
//設(shè)置輸入框輸入類型為 文本輸入
etLogin.setInputType(EditorInfo.TYPE_CLASS_TEXT);
//清空輸入框內(nèi)容
etLogin.setText("");
etPassword.setText("");
//設(shè)置用戶名登錄按鈕文字為 手機號登錄
btnPhoneLogin.setText("手機號登錄");
//設(shè)置 TextInputLayout 文字為 用戶名和密碼
tilLogin.setHint("用戶名");
tilPassword.setHint("密碼");
}
2.3、手機號登錄實現(xiàn)
如上按灶,實現(xiàn)邏輯鸯旁,登錄請求網(wǎng)絡(luò)接下來介紹。
//手機號登錄方法
public void phoneLoginViewinit() {
//用戶名登錄控件初始化
phoneLoginTrans();
//這里只是用做切換界面残炮,用戶名登錄情況下點擊按鈕顯示到手機號登錄势就,顯示獲取驗證碼按鈕
btnPhoneLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//轉(zhuǎn)換為用戶名登錄界面
userNameLoginViewInit();
}
});
//登錄按鈕監(jiān)聽
btnLogin.setOnClickListener(this);
}
//手機號登錄控件顯示
private void phoneLoginTrans() {
//將手機號登錄設(shè)置為 true
isPhoneLogin = true;
//顯示獲取驗證碼按鈕
tvVerifyCode.setVisibility(View.VISIBLE);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(250);
tvVerifyCode.setAnimation(alphaAnimation);
//設(shè)定點擊優(yōu)先級于最前(避免被EditText遮擋的情況)
tvVerifyCode.bringToFront();
//設(shè)置輸入框輸入類型為 手機號
etLogin.setInputType(EditorInfo.TYPE_CLASS_PHONE);
//清空輸入框內(nèi)容
etLogin.setText("");
etPassword.setText("");
//手機號登錄按鈕文字改為 用戶名登錄
btnPhoneLogin.setText("用戶名登錄");
//設(shè)置 TextInputLayout 文字為 手機號和密碼
tilLogin.setHint("手機號");
tilPassword.setHint("密碼");
}
2.4苞冯、請求網(wǎng)絡(luò)實現(xiàn)登錄
2.4.1抱完、用戶名登錄
之前介紹的工具還沒用上呢刃泡,現(xiàn)在來請求網(wǎng)絡(luò)烘贴,看看需要那些數(shù)據(jù)桨踪,請求參數(shù)有登錄方式 action芹啥,用戶名 userName 和 密碼 password墓怀。
2.4.2傀履、為如上數(shù)據(jù)創(chuàng)建一個請求實體類钓账。
public class LoginRequest extends IRequest {
public LoginRequest(int requestId, String userName, String password) {
mRequestId = requestId;
mParams.put("action", "login");
mParams.put("userName", userName);
mParams.put("password", password);
}
//getHost():返回的 API梆暮,這里在封裝 OkHttp 框架的時候有的,HOST_PUBLIC = "http://live.demo.cniao5.com/Api/";
@Override
public String getUrl() {
return getHost() + "User";
}
@Override
public Type getParserType() {
return new TypeToken<Response<UserInfo>>() {
}.getType();
}
}
2.4.3偿荷、網(wǎng)絡(luò)請求實現(xiàn)用戶名登錄
public void usernameLogin(final String userName, final String password) {
//檢查網(wǎng)絡(luò)狀態(tài)遭顶,需要添加網(wǎng)絡(luò)使用權(quán)限:ACCESS_NETWORK_STATE
if (checkUserNameLogin(userName, password)) {
// RequestComm.login 是 requestId:值是120棒旗,后臺提供
LoginRequest req = new LoginRequest(RequestComm.login, userName, password);
AsyncHttp.instance().postJson(req, new AsyncHttp.IHttpListener() {
@Override//請求網(wǎng)絡(luò)之前調(diào)用
public void onStart(int requestId) {
//顯示環(huán)形進度條
showOnLoading(true);
}
@Override
public void onSuccess(int requestId, Response response) {
//RequestComm.SUCCESS 狀態(tài)驗證:值是0铣揉,上面請求網(wǎng)絡(luò)有顯示
if (response.status == RequestComm.SUCCESS) {
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
}
//隱藏環(huán)形進度條
showOnLoading(false);
}
@Override
public void onFailure(int requestId, int httpStatus, Throwable error) {
showOnLoading(false);//隱藏環(huán)形進度條
Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
}
});
}
}
2.5逛拱、手機登錄實現(xiàn)
手機號登錄先來測試網(wǎng)絡(luò)數(shù)據(jù)API朽合,看看實體類需要的數(shù)據(jù)。請求參數(shù)有登錄方式 action宪彩,手機號 mobile 和 驗證碼 verifyCode尿孔。
2.5.1活合、創(chuàng)建手機登錄請求實體類
public class PhoneLoginRequest extends IRequest {
public PhoneLoginRequest(int requestId, String mobile, String verifyCode) {
mRequestId = requestId;
mParams.put("action", "phoneLogin");
mParams.put("mobile", mobile);
mParams.put("verifyCode", verifyCode);
}
//getHost():返回的 API白指,這里在封裝 OkHttp 框架的時候有的侵续,HOST_PUBLIC = "http://live.demo.cniao5.com/Api/";
@Override
public String getUrl() {
return getHost() + "User";
}
@Override
public Type getParserType() {
return new TypeToken<Response>() {
}.getType();
}
}
2.5.2状蜗、網(wǎng)絡(luò)請求實現(xiàn)手機號登錄
public void phoneLogin(final String mobile, String verifyCode) {
//檢查網(wǎng)絡(luò)狀態(tài)鹉动,需要添加網(wǎng)絡(luò)使用權(quán)限:ACCESS_NETWORK_STATE
if (checkPhoneLogin(mobile, verifyCode)) {//requestId = 1200,后臺提供
PhoneLoginRequest req = new PhoneLoginRequest(1200, mobile, verifyCode);
AsyncHttp.instance().postJson(req, new AsyncHttp.IHttpListener() {
@Override//請求網(wǎng)絡(luò)之前調(diào)用
public void onStart(int requestId) {
showOnLoading(true);//顯示環(huán)形進度條
}
@Override
public void onSuccess(int requestId, Response response) {
//RequestComm.SUCCESS 狀態(tài)驗證:值是0泽示,上面請求網(wǎng)絡(luò)有顯示
if (response.status == RequestComm.SUCCESS) {
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
}
showOnLoading(false);//隱藏環(huán)形進度條
}
@Override
public void onFailure(int requestId, int httpStatus, Throwable error) {
showOnLoading(false);//隱藏環(huán)形進度條
Toast.makeText(LoginActivity.this, "網(wǎng)絡(luò)異常", Toast.LENGTH_SHORT).show();
}
});
}
}
檢查用戶名登錄捎泻、手機登錄、環(huán)形進度條顯示或隱藏代碼
//監(jiān)測用戶名登錄
public boolean checkUserNameLogin(String userName, String password) {
if (OtherUtils.isUsernameVaild(userName)) {
if (OtherUtils.isPasswordValid(password)) {
if (OtherUtils.isNetworkAvailable(this)) {
return true;
} else {
ToastUtils.showShort(this, "當前無網(wǎng)絡(luò)連接");
}
} else {
ToastUtils.showShort(this, "密碼過短");
}
} else {
ToastUtils.showShort(this, "用戶名不符合規(guī)范");
}
return false;
}
//監(jiān)測手機號登錄
public boolean checkPhoneLogin(String phone, String verifyCode) {
if (OtherUtils.isPhoneNumValid(phone)) {
if (OtherUtils.isVerifyCodeValid(verifyCode)) {
if (OtherUtils.isNetworkAvailable(this)) {
return true;
} else {
ToastUtils.showShort(this, "當前無網(wǎng)絡(luò)連接");
}
} else {
ToastUtils.showShort(this, "驗證碼錯誤");
}
} else {
ToastUtils.showShort(this, "手機格式錯誤");
}
return false;
}
//進度條顯示與隱藏
public void showOnLoading(boolean active) {
if (active) {
//顯示進度條
progressBar.setVisibility(View.VISIBLE);
//顯示登錄按鈕
btnLogin.setVisibility(View.INVISIBLE);
//輸入框設(shè)置不可用
etLogin.setEnabled(false);
etPassword.setEnabled(false);
//手機號按鈕和按鈕設(shè)置為不可點擊
btnPhoneLogin.setClickable(false);
btnRegister.setClickable(false);
btnRegister.setTextColor(getResources().getColor(R.color.colorTransparentGray)); btnPhoneLogin.setTextColor(getResources().getColor(R.color.colorTransparentGray));
} else {
//隱藏進度條
progressBar.setVisibility(View.GONE);
//隱藏登錄按鈕
btnLogin.setVisibility(View.VISIBLE);
//輸入框設(shè)置可用
etLogin.setEnabled(true);
etPassword.setEnabled(true);
//手機號按鈕和按鈕設(shè)置為可點擊
btnPhoneLogin.setClickable(true);
btnRegister.setClickable(true);
btnRegister.setTextColor(getResources().getColor(R.color.white));
btnPhoneLogin.setTextColor(getResources().getColor(R.color.white));
}
}
2.6闯狱、手機號登錄和用戶名登錄切換
@Override
public void onClick(View v) {
if (isPhoneLogin){
phoneLogin(etLogin.getText().toString(), etPassword.getText().toString());
}else {
usernameLogin(etLogin.getText().toString(), etPassword.getText().toString());
}
}
三哄孤、完整的代碼實現(xiàn)
public class LoginActivity extends BaseActivity implements View.OnClickListener {
private ProgressBar progressBar;
private EditText etPassword;
private EditText etLogin;
private Button btnLogin;
private Button btnPhoneLogin;
private TextInputLayout tilLogin, tilPassword;
private Button btnRegister;
private TextView tvVerifyCode;
private boolean isPhoneLogin = false;
@Override
protected void setActionBar() {
}
@Override
protected void setListener() {
}
@Override
protected void initData() {
}
@Override
protected void initView() {
etLogin = obtainView(R.id.et_username);
etPassword = obtainView(R.id.et_password);
btnRegister = obtainView(R.id.btn_register);
btnPhoneLogin = obtainView(R.id.btn_phone_login);
btnLogin = obtainView(R.id.btn_login);
progressBar = obtainView(R.id.progressbar);
tilLogin = obtainView(til_login);
tilPassword = obtainView(R.id.til_password);
tvVerifyCode = obtainView(R.id.btn_verify_code);
userNameLoginViewInit();
}
@Override
protected int getLayoutId() {
return R.layout.activity_login;
}
/**
* 用戶名密碼登錄界面
*/
public void userNameLoginViewInit() {
userLoginTrans();
btnRegister.setOnClickListener(this);
btnPhoneLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//手機號登錄
phoneLoginViewinit();
}
});
//用戶名登錄
btnLogin.setOnClickListener(this);
}
public void phoneLoginViewinit() {
phoneLoginTrans();
//轉(zhuǎn)換為用戶名登錄界面
userNameLoginViewInit();
}
});
btnLogin.setOnClickListener(this);
}
private void phoneLoginTrans() {
isPhoneLogin = true;
tvVerifyCode.setVisibility(View.VISIBLE);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(250);
tvVerifyCode.setAnimation(alphaAnimation);
tvVerifyCode.bringToFront();
etLogin.setInputType(EditorInfo.TYPE_CLASS_PHONE);
etLogin.setText("");
etPassword.setText("");
btnPhoneLogin.setText("用戶名登錄");
tilLogin.setHint("手機號");
tilPassword.setHint("密碼");
}
private void userLoginTrans() {
isPhoneLogin = false;
tvVerifyCode.setVisibility(View.GONE);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(250);
tvVerifyCode.setAnimation(alphaAnimation);
etLogin.setInputType(EditorInfo.TYPE_CLASS_TEXT);
etLogin.setText("");
etPassword.setText("");
btnPhoneLogin.setText("手機號登錄");
tilLogin.setHint("用戶名");
tilPassword.setHint("密碼");
}
/**
* 手機登錄和用戶名登錄界面顯示或隱藏
*/
public void showOnLoading(boolean active) {
if (active) {
progressBar.setVisibility(View.VISIBLE);
btnLogin.setVisibility(View.INVISIBLE);
etLogin.setEnabled(false);
etPassword.setEnabled(false);
btnPhoneLogin.setClickable(false);
btnRegister.setTextColor(getResources().getColor(R.color.colorTransparentGray)); btnPhoneLogin.setTextColor(getResources().getColor(R.color.colorTransparentGray));
btnRegister.setClickable(false);
} else {
progressBar.setVisibility(View.GONE);
btnLogin.setVisibility(View.VISIBLE);
etLogin.setEnabled(true);
etPassword.setEnabled(true);
btnPhoneLogin.setClickable(true);
btnRegister.setClickable(true);
btnRegister.setTextColor(getResources().getColor(R.color.white));
btnPhoneLogin.setTextColor(getResources().getColor(R.color.white));
}
}
public void phoneLogin(final String mobile, String verifyCode) {
if (checkPhoneLogin(mobile, verifyCode)) {
PhoneLoginRequest req = new PhoneLoginRequest(1200, mobile, verifyCode);
AsyncHttp.instance().postJson(req, new AsyncHttp.IHttpListener() {
@Override
public void onStart(int requestId) {
showOnLoading(true);
}
@Override
public void onSuccess(int requestId, Response response) {
if (response.status == RequestComm.SUCCESS) {
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
}
showOnLoading(false);
}
@Override
public void onFailure(int requestId, int httpStatus, Throwable error) {
showOnLoading(false);
Toast.makeText(LoginActivity.this, "網(wǎng)絡(luò)異常", Toast.LENGTH_SHORT).show();
}
});
}
}
public boolean checkPhoneLogin(String phone, String verifyCode) {
if (OtherUtils.isPhoneNumValid(phone)) {
if (OtherUtils.isVerifyCodeValid(verifyCode)) {
if (OtherUtils.isNetworkAvailable(this)) {
return true;
} else {
ToastUtils.showShort(this, "當前無網(wǎng)絡(luò)連接");
}
} else {
ToastUtils.showShort(this, "驗證碼錯誤");
}
} else {
ToastUtils.showShort(this, "手機格式錯誤");
}
return false;
}
public void usernameLogin(final String userName, final String password) {
if (checkUserNameLogin(userName, password)) {
LoginRequest req = new LoginRequest(RequestComm.login, userName, password);
AsyncHttp.instance().postJson(req, new AsyncHttp.IHttpListener() {
@Override
public void onStart(int requestId) {
showOnLoading(true);
}
@Override
public void onSuccess(int requestId, Response response) {
if (response.status == RequestComm.SUCCESS) {
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
}
showOnLoading(false);
}
@Override
public void onFailure(int requestId, int httpStatus, Throwable error) {
showOnLoading(false);
Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
}
});
}
}
public boolean checkUserNameLogin(String userName, String password) {
if (OtherUtils.isUsernameVaild(userName)) {
if (OtherUtils.isPasswordValid(password)) {
if (OtherUtils.isNetworkAvailable(this)) {
return true;
} else {
ToastUtils.showShort(this, "當前無網(wǎng)絡(luò)連接");
}
} else {
ToastUtils.showShort(this, "密碼過短");
}
} else {
ToastUtils.showShort(this, "用戶名不符合規(guī)范");
}
return false;
}
@Override
public void onClick(View v) {
if (isPhoneLogin){
phoneLogin(etLogin.getText().toString(), etPassword.getText().toString());
}else {
usernameLogin(etLogin.getText().toString(), etPassword.getText().toString());
}
}
}
140套Android優(yōu)秀開源項目源碼,領(lǐng)取地址:http://mp.weixin.qq.com/s/afPGHqfdiApALZqHsXbw-A
寫得不到位的地方還望大家指正。