Android 通过开源框架AsyncHttpClient进行get和post请求

使用时无需将这些代码放入子线程去执行,因为其内部已经封装到一个线程中运行了!

public void asyncHttpClientGet(View view) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(
                "http://10.0.2.2:8080/LoginServlet?username=张三&password=123",
                new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(String response) {
                        Toast.makeText(MainActivity.this, response,
                                Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public void asyncHttpClientPost(View view) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams requestParams = new RequestParams();
        requestParams.add("username", "张三");
        requestParams.add("password", "123");
        client.post("http://10.0.2.2:8080/LoginServlet", requestParams,
                new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(String response) {
                        Toast.makeText(MainActivity.this, response,
                                Toast.LENGTH_SHORT).show();
                    }
                });
    }

我所用的是1.4.4版本的jar包

原文地址:https://www.cnblogs.com/wuyou/p/3427901.html