zoukankan      html  css  js  c++  java
  • 异步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();
            }
    
        }
  • 相关阅读:
    linux下文件搜索命令学习笔记
    【转】C++格式化输出
    UNIX中的文件类型
    Unix内核中打开文件的表示
    网络编程学习笔记:linux下的socket编程
    TCP协议学习笔记(一)首部以及TCP的三次握手连接四次挥手断开
    C/C++源代码从写完到运行发生了什么
    C++ 函数形参发生动态绑定时指针增长步长与静态类型一致
    C++中为什么要将析构函数定义成虚函数
    C++求一个十进制的二进制中1的个数
  • 原文地址:https://www.cnblogs.com/ahwu/p/3392740.html
Copyright © 2011-2022 走看看