*****
**普通方式寫代碼**
[TOC=6]
# 1. 普通方式寫代碼
## 1.1 什么是普通方式寫代碼
剛開始學習,我們沒有學習任何架構模式,隨意按順序寫代碼。
## 1.2 登錄案例

### 1.2.1 布局文件
~~~
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<!-- 用戶名輸入框 -->
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入用戶名" />
<!-- 密碼輸入框 -->
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_username"
android:hint="請輸入密碼" />
<!-- 登錄按鈕 -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_password"
android:onClick="login"
android:text="登錄" />
</RelativeLayout>
~~~
### 1.2.2 Activity代碼
~~~
package cn.zhaoliang5156.day03normal;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.common.io.CharStreams;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import cn.zhaoliang5156.day03normal.bean.User;
/**
* Copyright (C), 2015-2019, 八維集團
* Author: zhaoliang
* Date: 2019/5/10 11:26 AM
* Description: 登錄界面
*
* phone:15169707777
* pwd:123456
*/
public class MainActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
}
/**
* 點擊登錄按鈕調用
*
* @param view
*/
public void login(View view) {
// 1. 從界面讀取用戶名和密碼 封裝成User對象
User user = inputToUser();
// 2. 檢測用戶用戶名和密碼
boolean isActiveUser = checkUser(user);
// 3. 請求服務器login登錄
if (isActiveUser) {
login(user);
}
}
/**
* 請求網絡接口登錄
*
* @param user
*/
private void login(final User user) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... strings) {
// 訪問網絡
HttpURLConnection connection = null;
try {
URL url1 = new URL(strings[0]);
connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setDoOutput(true);
String body = "phone=" + URLEncoder.encode(user.getUsername()) + "&pwd=" + URLEncoder.encode(user.getPassword());
connection.getOutputStream().write(body.getBytes());
// 判斷是否請求成功
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// guava
return CharStreams.toString(new InputStreamReader(connection.getInputStream()));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
// 回來的字符串
// 4. 讀取服務器響應更新UI
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
}.execute("http://172.17.8.100/small/user/v1/login");
}
/**
* 檢測用戶是否合格
*
* @param user
* @return
*/
private boolean checkUser(User user) {
if (!TextUtils.isEmpty(user.getUsername()) && !TextUtils.isEmpty(user.getPassword())) {
return true;
}
return false;
}
/**
* 把讀取的信息封裝成user
*
* @return
*/
private User inputToUser() {
User user = new User();
user.setUsername(etUsername.getText().toString());
user.setPassword(etPassword.getText().toString());
return user;
}
}
~~~
### 1.2.3 User bean類
~~~
package cn.zhaoliang5156.day03normal.bean;
/**
* Copyright (C), 2015-2019, 八維集團
* Author: zhaoliang
* Date: 2019/5/10 11:28 AM
* Description: 用戶bean
*/
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
~~~
- 班規
- 第一單元 Git
- 1.1 Git簡介
- 1.2 Git安裝
- 1.3 版本控制
- 1.4 遠程倉庫
- 1.5 分支管理
- 1.6 Git命令總結
- 1.7 在Android Studio中使用Git
- 第一單元 作業
- 第二單元 項目立項
- 2.1 需求文檔
- 2.2 原型圖
- 2.3 接口文檔
- 2.4 項目實現
- 2.5 制定開發計劃
- 第二單元 作業
- 第三單元 MVP搭建項目框架
- 3.1 代碼架構模式
- 3.2 普通方式寫代碼
- 3.3 使用MVC重構代碼
- 3.4 使用MVP重構代碼
- 3.5 使用接口提高代碼通用性
- 3.6 內存泄漏
- 3.7 使用契約統一管理接口
- 第三單元 作業
- 第四單元 MVP架構優化
- 4.1 MVP基類封裝與泛型應用
- 4.3 BaseActivity的封裝
- 第五單元 Volley網絡框架
- 5.1 Volley網絡框架
- 第六單元 Glide實現圖片異步加載
- 6.1 開始使用
- 6.2 占位符
- 6.3 Glide緩存
- 6.4 GlideGifVideo與色彩模式
- 第七單元 傳統屏幕適配
- 7.1 相對布局
- 7.1 權重
- 7.3 .9Patch
- 7.4 dimens適配
- 7.5 國際化
- 7.6 shape實現自定義樣式
- 7.7 自定義樣式
- 7.8 沉浸式狀態欄
- 第八單元 RecyclerView
- 8.1 RecycleView
- 第九單元 自定義View
- 第十單元 自定義View實戰
- 第十一單元 自定義View進階
- 第二十單元 屬性動畫
- 第十三單元 異常捕獲機制
- 第十四單元 原生登錄、注冊模塊
- 第十五單元 第三方登錄、分享、統計
- 第十六單元 HTML5新特性
- 第十七單元 CSS3新特性
- 第十八單元 WebView與JS交互
- 第一周周考
- 第二周周考
- 第三周周考
- 月考