Android核心技術-day05-01-類QQ登陸Post方式 及 使用第三方庫的方式
阿新 • • 發佈:2018-11-07
package com.gaozewen.qqloginpost; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.gaozewen.qqloginpost.utils.StreamUtils; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class MainActivity extends AppCompatActivity { private static final int GET_SERVER_RESP = 1; private static final int GET_ERROR = 2; private EditText mEt_qqNumber; private EditText mEt_password; private CheckBox mCb_remember; private Button mBt_login; /** * 1.定義一個共享引數(存放資料方便的 api) */ private SharedPreferences sp; // 新建 res/drawable-hdpi @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 2. 通過上下文得到一個共享引數的例項物件 sp = this.getSharedPreferences("config", MODE_PRIVATE); mEt_qqNumber = findViewById(R.id.et_qqnumber); mEt_password = findViewById(R.id.et_password); mCb_remember = findViewById(R.id.cb_remember); mBt_login = findViewById(R.id.bt_login); readStore(); } /** * 從 sp 檔案中讀取資訊 */ private void readStore() { // data/data/包名/shared_prefs String qq = sp.getString("qq", ""); String password = sp.getString("password", ""); mEt_qqNumber.setText(qq); mEt_password.setText(password); mCb_remember.setChecked(true); } // 登陸按鈕的點選事件 public void login(View view) { final String qq = mEt_qqNumber.getText().toString().trim(); final String password = mEt_password.getText().toString().trim(); if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(password)) { Toast.makeText(this, "使用者名稱和密碼不能為空", Toast.LENGTH_SHORT).show(); } else { // 判斷是否要記錄使用者名稱和密碼 if (mCb_remember.isChecked()) { // 被選中狀態需要記錄使用者名稱和密碼 // 3. 將資料儲存到 sp 檔案中 SharedPreferences.Editor edit = sp.edit(); edit.putString("qq", qq); edit.putString("password", password); edit.apply(); // apply() 非同步開個執行緒執行 commit() 同步 立即執行 } // 發請求時禁用 button mBt_login.setEnabled(false); new Thread() { @Override public void run() { try { Thread.sleep(5000); String path = String.format("http://192.168.1.102:8080/login?username=%s&password=%s", qq, password); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); // 設定傳送資料為表單型別,會被新增到 HTTP body 中 conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); String data = String.format("username=%s&password=%s",URLEncoder.encode(qq,"utf-8"),URLEncoder.encode(password,"utf-8")); conn.setRequestProperty("Content-Length", String.valueOf(data.length())); // 指定請求輸出模式 conn.setDoOutput(true); // 運行當前的應用程式給伺服器寫資料 conn.getOutputStream().write(data.getBytes()); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); String result = StreamUtils.readStream(is); showToastInAnyThread(result); } else { showToastInAnyThread("請求失敗"); } } catch (Exception e) { e.printStackTrace(); showToastInAnyThread("請求失敗"); } } }.start(); } } /** * 在任何執行緒中都可以呼叫 函式 Toast 方法 * @param result */ private void showToastInAnyThread(final String result) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(),result, Toast.LENGTH_SHORT).show(); } }); } }
第三方庫
import com.gaozewen.qqloginother.loopj.android.http.AsyncHttpClient;
import com.gaozewen.qqloginother.loopj.android.http.AsyncHttpResponseHandler;
import com.gaozewen.qqloginother.loopj.android.http.RequestParams;
import org.apache.http.Header;