异步http开源框架使用(AsyncHttpClient)


public void click(View view) {
        AsyncHttpClient client = new AsyncHttpClient();
        try {
            String url = "http://url?name="
                    + URLEncoder.encode("aaa", "utf-8") + "&pass="
                    + URLEncoder.encode("bbb", "utf-8");
            client.get(url, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers,
                        byte[] responseBody) {
                    super.onSuccess(statusCode, headers, responseBody);

                    Toast.makeText(Main.this,
                            "请求成功" + new String(responseBody), 0).show();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers,
                        byte[] responseBody, Throwable error) {
                    // TODO Auto-generated method stub
                    super.onFailure(statusCode, headers, responseBody, error);
                }
            });
        } catch (Exception e) {
        }

    }

    public void postclick(View view) {
        try {
            AsyncHttpClient client = new AsyncHttpClient();
            String url = "http://url";
            RequestParams params = new RequestParams();
            params.put("name", "张三");
            params.put("pass", "123456");
            client.post(url, params, new AsyncHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers,
                        byte[] responseBody) {
                    super.onSuccess(statusCode, headers, responseBody);
                    Toast.makeText(Main.this,
                            "请求成功" + new String(responseBody), 0).show();
                }

            });

        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void btnupfile(View view) {

        AsyncHttpClient client = new AsyncHttpClient();

        RequestParams params = new RequestParams();

        try {
            File file = new File("/sdcard/1.jpg");
            System.out.println(file);
            params.put("pic", file);

            client.post("http://url", params,
                    new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers,
                                byte[] responseBody) {
                            super.onSuccess(statusCode, headers, responseBody);
                            Toast.makeText(Main.this,
                                    "上传文件成功!", 0).show();
                        }
                    });

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM