标签:void 简单 avl ice 功能 rri create end blob
Retrofit+Okhttp辅助类的简单封装,vesion 1.0.X 实现了Get,Post-Form、Post-Json
三种形式的网络请求,后续版本会实现文件上传下载and各类raw的请求方式。
implementation ‘cn.cyq.net:retrofitutils:1.0.3‘
<!--library中引用了下面五个库,我没有打包进去了,避免版本冲突,比如七牛云的okio okhttp冲突-->
//网络请求依赖
implementation ‘com.squareup.okio:okio:1.14.0‘
implementation ‘com.squareup.okhttp3:okhttp:3.10.0‘
implementation ‘com.squareup.retrofit2:retrofit:2.4.0‘
implementation ‘com.squareup.retrofit2:converter-scalars:2.3.0‘
//Loader依赖
implementation ‘com.wang.avi:library:2.1.3‘
在Application的onCreate()初始化
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
RestClient.init(getApplicationContext(), "baseUrl address");
}
}
RestClient.buider()
.loader(this)//可以不添加
.url(url)
.success(new ISuccess() {
@Override
public void onSuccess(String response) {
Log.i("test", "GET请求:" + response);
}
})
.failure(new IFailure() {
@Override
public void onFailure() {
Log.i("test", "失败");
}
})
.error(new IError() {
@Override
public void onError(int code, String msg) {
Log.i("test", "错误");
}
})
.build()
.get();
RestClient.buider()
.loader(this)
.url(url)
.params("key1", "value1")
.params("key2", "value2")
.success(new ISuccess() {
@Override
public void onSuccess(String response) {
Log.i("test", "POST请求:" + response);
}
})
.failure(new IFailure() {
@Override
public void onFailure() {
Log.i("test", "失败");
}
})
.error(new IError() {
@Override
public void onError(int code, String msg) {
Log.i("test", "错误");
}
})
.build()
.post();
String jsonStr = "{\"username\":\"张三\",\"age\":16}";
RestClient.buider().loader(this)
.url("http://192.168.0.1:8080/service/jsontest.html")
.raw(jsonStr)
.success(new ISuccess() {
@Override
public void onSuccess(String response) {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
Log.i("test", "Post Row请求:" + response);
}
})
.error(new IError() {
@Override
public void onError(int code, String msg) {
Log.i("test", "Post Ro请求失败");
}
})
.build()
.post();
ps:2018-07-26[最新]
标签:void 简单 avl ice 功能 rri create end blob
原文地址:https://www.cnblogs.com/chenyangqi/p/9376924.html