博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络_AsyncHttpClient_使用方法和代码
阅读量:4290 次
发布时间:2019-05-27

本文共 2463 字,大约阅读时间需要 8 分钟。

AsyncHttpClient的简单程度,让人感到呕吐。灰常简单。
//AsyncHttpClient is an open-source project made by developers for developers!
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. You can also
use it in Service or background thread, library will automatically recognize in which context is ran.
各种对响应体的包装处理回调类: 
AsyncHttpResponseHandler;
BaseJsonHttpResponseHandler;
BinaryHttpResponseHandler;
DataAsyncHttpResponseHandler;
FileAsyncHttpResponseHandler;
JsonHttpResponseHandler;
// get 请求   类似post请求,类似上传加入RequestParams。
AsyncHttpClient client = new AsyncHttpClient();client.get("https://www.google.com", new AsyncHttpResponseHandler() {    public void onStart() {}    public void onSuccess(int statusCode, Header[] headers, byte[] response) {}    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {}    public void onRetry(int retryNo) {}});
//官方推荐配置。
public class TwitterRestClient {  private static final String BASE_URL = "https://api.twitter.com/1/";  private static AsyncHttpClient client = new AsyncHttpClient();  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {      client.get(getAbsoluteUrl(url), params, responseHandler);  }  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {      client.post(getAbsoluteUrl(url), params, responseHandler);  }  private static String getAbsoluteUrl(String relativeUrl) {      return BASE_URL + relativeUrl;  }}

//上传。文件上传
加入post中。
//输入流上传InputStream myInputStream = blah;RequestParams params = new RequestParams();params.put("secret_passwords", myInputStream, "passwords.txt");//本件上传。可以多个文件File myFile = new File("/path/to/file.png");RequestParams params = new RequestParams();params.put("profile_picture", myFile);//二进制字节流上传byte[] myByteArray = blah;RequestParams params = new RequestParams();params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");

//下载
AsyncHttpClient client = new AsyncHttpClient();client.get(new File("存储的位置"), new FileAsyncHttpResponseHandler(/* Context */ this) {    @Override    public void onSuccess(int statusCode, Header[] headers, File response) {        // Do something with the file `response`    }});

转载地址:http://hbegi.baihongyu.com/

你可能感兴趣的文章
CAS原子操作实现无锁及性能分析
查看>>
Linux 互斥锁、原子操作实现原理
查看>>
搭建简单hls直播测试服务
查看>>
共享内存的数据同步
查看>>
Cache和Buffer的区别
查看>>
50个sql语句
查看>>
MYSQL sql 语句性能分析
查看>>
C++操作Redis数据库
查看>>
python yield用法
查看>>
python pipe模块用法
查看>>
安装完 MySQL 后必须调整的 10 项配置
查看>>
开发者必备的 12 个 JavaScript 库
查看>>
http错误码
查看>>
python 多线程
查看>>
sipp命令 各参数含义
查看>>
搜集的动植物分类、检索网站
查看>>
ffmpeg源码分析之媒体打开过程
查看>>
Ubuntu/centos/redhat/SUSE sipp安装(带rtp支持,3.5.1版本)
查看>>
周鸿祎:很多程序员聪明,但我一看就知道他不会成功
查看>>
编译程序遇到问题 relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
查看>>