zoukankan      html  css  js  c++  java
  • 08_android入门_android-async-http开源项目介绍及用法

    android-async-http开源项目可以是我们轻松的获取网络数据或者向server发送数据。使用起来很easy,关于android-async-http开源项目的介绍内容来自于官方:http://loopj.com/android-async-http/.以下我对此主页上内容进行大体上的翻译,希望可以对你理解android-async-http开源项目有所帮助

    1.1 Overview(概况)

         An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing.

    译文:

      异步基于回调的Httpclient为Android构建。是基于Apache HttpClient库的。全部的请求都是位于应用程序主线程 UI 之外,但不论什么回调逻辑将同样的线程上运行回调,使用Android的处理程序创建消息传递。

    1.2 Features(特征)

    • Make asynchronous HTTP requests, handle responses in anonymous callbacks
    • 进行异步HTTP请求,处理响应在匿名回调中完毕
    • HTTP requests happen outside the UI thread
    • HTTP请求发生在UI线程之外
    • Requests use a threadpool to cap concurrent resource usage
    • 请求使用threadpool。限制并发资源使用情况
    • GET/POST params builder (RequestParams)
    • GET / POST參数构建使用(RequestParams)
    • Multipart file uploads with no additional third party libraries
    • Multipart 文件上传,没有额外的第三方库
    • Tiny size overhead to your application, only 25kb for everything
    • 在你的应用程序上利用非常小的开销,只25 kb就能够做一切
    • Automatic smart request retries optimized for spotty mobile connections
    • 自己主动智能请求重试,优化了质量不一的移动连接
    • Automatic gzip response decoding support for super-fast requests
    • 自己主动解码支持gzip反应速度超快的请求
    • Binary file (images etc) downloading with BinaryHttpResponseHandler
    • 二进制文件(图片等)的下载,使用BinaryHttpResponseHandler
    • Built-in response parsing into JSON with JsonHttpResponseHandler
    • 内置响应解析成JSON,使用JsonHttpResponseHandler
    • Persistent cookie store, saves cookies into your app’s SharedPreferences
    • 持久化cookie存储。保存cookie到你的应用程序的SharedPreferences

    2.Installation & Basic Usage(安装和基本使用方法)

         Download the latest .jar file from github and place it in your Android app’s libs/ folder.

         从github上下载最新的最新的jar文件.并将其放置在你的Android应用程序的libs /目录.

     2.1下载方式:

        1.从http://loopj.com/android-async-http/的页面下载

    点击DownLoad就可以下载最新的jar文件

       2.从https://github.com/loopj/android-async-http的页面下载


    找到DownLoad ZIP进行下载文件,解压后的文件夹例如以下


    examples:里面有简单的样例

    library:里面存放的是android-async-http开源项目的源代码(方法一:能够把librarysrcmainjava文件以下的文件复制到。你应用的src下也能够直接使用)

    releases:里面存放的是各个版本号的jar文件。(方法二:仅仅需把最新的jar文件复制到你应用的libs文件夹下就可以.)

    samples:里面存放的也是样例(可供參考)

    备注:方法一和方法二仅仅能採用当中之中的一个。建议採用方法二

    2.2用法

      Import the http package.

    import com.loopj.android.http.*;
    

    Create a new AsyncHttpClient instance and make a request:

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://www.google.com", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            System.out.println(response);
        }
    });

    Adding GET/POST Parameters with RequestParams

    The RequestParams class is used to add optional GET or POST parameters to your requests.RequestParams can be built and constructed in various ways:

    Create empty RequestParams and immediately add some parameters:

    RequestParams params = new RequestParams();
    params.put("key", "value");
    params.put("more", "data");
    

    Create RequestParams for a single parameter:

    RequestParams params = new RequestParams("single", "value");
    

    Create RequestParams from an existing Map of key/value strings:

    HashMap<String, String> paramMap = new HashMap<String, String>();
    paramMap.put("key", "value");
    RequestParams params = new RequestParams(paramMap);
    

    See the RequestParams Javadoc for more information.

    Add an InputStream to the RequestParams to upload:

    InputStream myInputStream = blah;
    RequestParams params = new RequestParams();
    params.put("secret_passwords", myInputStream, "passwords.txt");
    

    Add a File object to the RequestParams to upload:

    File myFile = new File("/path/to/file.png");
    RequestParams params = new RequestParams();
    try {
        params.put("profile_picture", myFile);
    } catch(FileNotFoundException e) {}
    

    Add a byte array to the RequestParams to upload:

    byte[] myByteArray = blah;
    RequestParams params = new RequestParams();
    params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");
    

    See the RequestParams Javadoc for more information.

    Downloading Binary Data with BinaryHttpResponseHandler

    The BinaryHttpResponseHandler class can be used to fetch binary data such as images and other files. For example:

    AsyncHttpClient client = new AsyncHttpClient();
    String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
    client.get("http://example.com/file.png", new BinaryHttpResponseHandler(allowedContentTypes) {
        @Override
        public void onSuccess(byte[] fileData) {
            // Do something with the file
        }
    });
    

    See the BinaryHttpResponseHandler Javadoc for more information.

  • 相关阅读:
    [非专业翻译] Mapster
    [非专业翻译] Mapster
    排序之猴子算法
    1309游客统计
    1631低洼地
    1636车牌问题
    1638图形
    这是一篇小短文
    1500【自定义函数】走楼梯
    PHP 之表单提交大数据,数据不完整
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5221561.html
Copyright © 2011-2022 走看看