<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ***** **普通方式寫代碼** [TOC=6] # 1. 普通方式寫代碼 ## 1.1 什么是普通方式寫代碼 剛開始學習,我們沒有學習任何架構模式,隨意按順序寫代碼。 ## 1.2 登錄案例 ![](https://box.kancloud.cn/bbdd058bd290c6b038e30e8a95511e09_1080x1920.png) ### 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; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看