android 學習隨筆十二(網路:使用非同步HttpClient架構)

來源:互聯網
上載者:User

標籤:

使用非同步HttpClient架構發送get、post請求

在https://github.com/  搜尋 asyn-http https://github.com/search?utf8=?&q=asyn-http 下載 loopj/android-async-http

public class MainActivity extends Activity {    Handler handler = new Handler(){        public void handleMessage(android.os.Message msg) {            Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void click1(View v){        //擷取使用者輸入的帳號密碼        EditText et_name = (EditText) findViewById(R.id.et_name);        EditText et_pass = (EditText) findViewById(R.id.et_pass);            String name = et_name.getText().toString();        String pass = et_pass.getText().toString();                String path = "http://169.254.244.136/Web2/servlet/Login";        //使用非同步HttpClient發送get請求        AsyncHttpClient client = new AsyncHttpClient();                //把要提交的資料封裝在rp對象中        RequestParams rp = new RequestParams();        rp.put("name", name);        rp.put("pass", pass);        //發送get請求        client.get(path, rp, new MyHandler());    }        class MyHandler extends AsyncHttpResponseHandler{        //請求成功時回調,相應碼為200        @Override        public void onSuccess(int statusCode, Header[] headers,                byte[] responseBody) {            Toast.makeText(MainActivity.this, new String(responseBody), 0).show();        }        //請求失敗時回調,相應碼不為200        @Override        public void onFailure(int statusCode, Header[] headers,                byte[] responseBody, Throwable error) {            Toast.makeText(MainActivity.this, "請求失敗", 0).show();        }            }            public void click2(View v){        //擷取使用者輸入的帳號密碼        EditText et_name = (EditText) findViewById(R.id.et_name);        EditText et_pass = (EditText) findViewById(R.id.et_pass);            String name = et_name.getText().toString();        String pass = et_pass.getText().toString();                String path = "http://169.254.244.136/Web2/servlet/Login";                //使用非同步HttpClient發送post請求        AsyncHttpClient client = new AsyncHttpClient();                //把要提交的資料封裝在rp對象中        RequestParams rp = new RequestParams();        rp.put("name", name);        rp.put("pass", pass);        //發送get請求        client.post(path, rp, new MyHandler());    }    }

 

android 學習隨筆十二(網路:使用非同步HttpClient架構)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.