1. 程式人生 > >Android asynchttpclient方式提交資料(GitHub開源)

Android asynchttpclient方式提交資料(GitHub開源)

package com.example.a37_asynchttplient;


import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import cz.msebera.android.httpclient.Header;


public class MainActivity extends AppCompatActivity {

    private EditText et_username;
    private EditText et_password;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1 找到我們關心的控制元件
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
    }
    //點選按鈕 進行Get方式提交資料
    public void click1(View v) {
        String name = et_username.getText().toString().trim();
        String pwd = et_password.getText().toString().trim();
        //2.1 定義get方式要提交的路徑 小細節 如果提交中文要對name和pwd進行一個urlencode編碼
        try {
            path = "http://192.168.11.73:8080/login/LoginServlet?username="+ URLEncoder.encode(name, "utf-8")+"&password="+ URLEncoder.encode(pwd, "utf-8")+"";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //3 使用開源專案進行get請求
        //3.1 建立asynchttpclient
        AsyncHttpClient client = new AsyncHttpClient();
        //3.2 進行get請求
        client.get(path, new AsyncHttpResponseHandler() {
            //請求成功的回撥方法
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                try {
                    Toast.makeText(getApplicationContext(), new String(responseBody, "gbk"), Toast.LENGTH_SHORT).show();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            //請求失敗
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(getApplicationContext(), "伺服器忙", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //1 點選按鈕 進行Post方式提交資料
    public void click2(View v) {
        //2 獲取使用者名稱和密碼
        String name = et_username.getText().toString().trim();
        String pwd = et_password.getText().toString().trim();
        String path = "http://192.168.11.73:8080/login/LoginServlet";
        //3.1 建立asynchttpclient
        AsyncHttpClient client = new AsyncHttpClient();
        //3.1.0 準備請求體的內容
        RequestParams params = new RequestParams();
        params.put("username", name);
        params.put("password", pwd);
        //3.2 進行post請求 params請求的引數封裝
        client.post(path, params, new AsyncHttpResponseHandler() {
            // 請求成功 登入成功
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                try {
                    Toast.makeText(getApplicationContext(), new String(responseBody,"gbk"), Toast.LENGTH_SHORT).show();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(getApplicationContext(), "伺服器忙", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
去Github上找asynchttpclient的開源庫 https://github.com/loopj/android-async-http