Android网络请求框架五、Ion

简述: 
Ion支持网络请求和进行图片加载的双重功能
拥有链式api风格(Fluent API)
当Activity结束的时候支持自动的取消操作
支持SPDY/HTTP2,缓存,Gzip压缩,HTTP连接的重用等
并且是基于AndroidAsync实现的,AndroidAsync是作者的另一个使用socket实现的,遵循http协议的类库
作者网站:
http://www.koushikdutta.com/
http://koush.com
Github主页: https://github.com/koush/ion
ion基于Async: https://github.com/koush/AndroidAsync
AndroidAsync is a low level network protocol library. If you are looking for an easy to use, higher level, Android aware, http request library, check out Ion (it is built on top of AndroidAsync). The typical Android app developer would probably be more interested in Ion.
But if you're looking for a raw Socket, HTTP client/server, WebSocket, and Socket.IO library for Android, AndroidAsync is it.
下载源码: 
http://maven.outofmemory.cn/com.koushikdutta.ion/ion/1.3.7/
依赖com.koushikdutta.async 
http://maven.outofmemory.cn/com.koushikdutta.async/
还需要编译成class版本或者解码到项目里编译

添加依赖
dependencies {
    compile 'com.koushikdutta.ion:ion:2.+'
}
使用ion进行get请求
  Ion.with(this)
   .load(Api.TEST)
   .asString()//以字符串形式返回,服务器返回的都是流,只是类库将流转为字符串了
   .setCallback(callback);//设置接收结果的回调接口
    FutureCallback<String> callback = new FutureCallback<String>() {
    /**
     * 回调是在主线程执行的,所以可以直接更新UI
     * @param e
     * @param result
     */
    @Override
    public void onCompleted(Exception e, String result) {
        text.setText(e == null ? result : e.getMessage());
    }
};
使用ion进行post请求,提交key-value形式的参数
Ion.with(this)
   .load(Api.LOGIN)
   .setBodyParameter("username","俊哥")//设置请求参数
   .setBodyParameter("password","123")
   .asString()
   .setCallback(callback);
使用ion进行post请求,提交json对象参数
//构造json对象
JsonObject json = new JsonObject();
json.addProperty("city","北京");
json.addProperty("year","2016");
Ion.with(this)
   .load(Api.POST_JSON)
   .setJsonObjectBody(json)
   .asString()
   .setCallback(callback);
使用ion进行上传文件,并显示进度
 File file = new File(Environment.getExternalStorageDirectory(),"dog.jpg");
Ion.with(this)
        .load(Api.UPLOAD)
        .uploadProgress(new ProgressCallback() {
            @Override
            public void onProgress(long downloaded, long total) {
                int percent = (int) (downloaded*100f/total+0.5f);
                Log.e("tag","上传进度:"+percent+"%");
            }
        })
        .setMultipartFile("file",file)
        .asString()
        .setCallback(callback);
使用ion进行下载文件,并且显示进度
File file = new File(Environment.getExternalStorageDirectory(),"a.jpg");
Ion.with(this)
        .load(Api.IMAGE)
        .progress(new ProgressCallback() {
            @Override
             public void onProgress(long downloaded, long total) {
                int percent = (int) (downloaded*100f/total+0.5f);
                text.setText("下载进度:"+percent+"%");
            }
        })
        .write(file)
        .setCallback(new FutureCallback<File>() {
            @Override
            public void onCompleted(Exception e, File file) {
                text.setText("下载成功:"+file.getAbsolutePath());
            }
        });

猜你喜欢

转载自blog.csdn.net/u010144805/article/details/81352624