Android 中 OkGo 的使用 (封装 OkHttp)
生活随笔
收集整理的這篇文章主要介紹了
Android 中 OkGo 的使用 (封装 OkHttp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目介紹
github地址:https://github.com/jeasonlzy/okhttp-OkGo
wiki:https://github.com/jeasonlzy/okhttp-OkGo/wiki
配置
般來說,只需要添加第一個okgo的核心包即可,其余的三個庫根據自己的需要選擇添加!!!
//必須使用
compile 'com.lzy.net:okgo:3.0.4'
//以下三個選擇添加,okrx和okrx2不能同時使用
compile 'com.lzy.net:okrx:1.0.2'
compile 'com.lzy.net:okrx2:2.0.2'
compile 'com.lzy.net:okserver:2.0.5'
基本get和post請求
get請求:
OkGo.<String>get("https://api.github.com/repos/square/retrofit/contributors") // 請求方式和請求url
.tag(this) // 請求的 tag, 主要用于取消對應的請求
.cacheKey("cacheKey") // 設置當前請求的緩存key,建議每個不同功能的請求設置一個
.cacheMode(CacheMode.NO_CACHE) // 緩存模式,詳細請看緩存介紹
// .cacheTime(3000)//緩存時間
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
Log.i("get",response.body());
tvText.setText(response.body());
}
@Override
public void onError(Response<String> response) {
super.onError(response);
}
});
post請求:
OkGo.<String>post("url")
.tag(this)
.cacheKey("cachePostKey")
.cacheMode(CacheMode.NO_CACHE)
.params("shopperId", "9356")
.params("machineId", "5117")
.params("orderType", "2")
.params("orderId", "108")
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
Log.i("aaa",response.body());
Log.i("time2",System.currentTimeMillis()+"s");
BorrowRecordInfo borrowRecordInfo = JSON.parseObject(response.body(),BorrowRecordInfo.class);
if (borrowRecordInfo != null){
tvText.setText(response.body());
Toast.makeText(Demo1.this,borrowRecordInfo.getOrder().getNickName(),Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(Response<String> response) {
super.onError(response);
}
});
總結
以上是生活随笔為你收集整理的Android 中 OkGo 的使用 (封装 OkHttp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人工智能、机器学习、深度学习和神经网络的
- 下一篇: 神经网络知识整理