android---(async-http)

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

/**
     * 发送一个http请求
     *
     * @param view
     */
    public void sendSimpleGet(View view) {

        //客户端请求
        AsyncHttpClient client = new AsyncHttpClient();

        client.get("http://www.baidu.com", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                //200 ok
                String info = new String(responseBody);
                System.out.print(info);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    /**
     * 发送一个带 参数请求的 http
     *
     * @param view
     */
    public void sendParams(View view) {
        AsyncHttpClient client = new AsyncHttpClient();

        //请求参数
        RequestParams params = new RequestParams();
        params.put("username", "zhang");
        params.put("password", "123");

        client.post(this, "http://www.baidu.com", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }


    /**
     * 文件 上传功能
     *
     * @param view
     */
    public void upload(View view) {
        AsyncHttpClient client = new AsyncHttpClient();

        RequestParams params = new RequestParams();

        //设置文件
        try {
            String path =
                    //获取公共文件夹 pictures/c.jgp 文件路径
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/c.jgp";

            //参数:文件名,路径,文件类型
            params.put("fileName", new File(path), "image/jpeg");

            //描述:对应服务段的字段
            params.put("description", "美女图片");

            String url = "http://10.0.2.2:8080/servletUpload";
            client.post(this, url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                }
            });


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    public void downLoad(View view) {
        AsyncHttpClient client = new AsyncHttpClient();
        String url = "http://i6.topit.me/6/5d/45/1131907198420455d6o.jpg";

        //下载-缓存目录-读取
        client.get(url, new FileAsyncHttpResponseHandler(this) {
            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
                //error

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, File file) {
                try {
                    InputStream in = new FileInputStream(file);

                    String path =
                            Environment.getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_PICTURES) + "/1131907198420455d6o.jpg";

                    OutputStream out = new FileOutputStream(path);

                    byte[] bytes = new byte[100];
                    int len = -1;
                    while ((len = in.read(bytes)) != -1) {
                        out.write(bytes, 0, len);
                        out.flush();

                    }
                    out.close();


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    /**
     * json 请求
     *
     * @param view
     */
    public void jsonHttp(View view) {

        AsyncHttpClient client = new AsyncHttpClient();

        String url = "http://www.baidu.com";
        JSONObject jsonObject = new JSONObject();
        try {

            jsonObject.put("username", "zhang");
            jsonObject.put("password", "p");

            StringEntity entity = new StringEntity(jsonObject.toString());

            client.post(this, url, entity, "application/json", new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    super.onSuccess(statusCode, headers, response);
                    //200 ok
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值