android okgo参数,Android OkGo基本操作

或许不是很全面,也都是从网上粘的,自己记下以后使用方便点。如有不对的地方 欢迎指教。

首先添加依赖:

implementation 'com.lzy.net:okgo:3.0.4'

设置初始化和全局配置:

public class App extends Application {

@Override

public void onCreate() {

super.onCreate();

//okGo网络框架初始化和全局配置

OkHttpClient.Builder builder = new OkHttpClient.Builder();

//builder.cookieJar(new CookieJarImpl(new SPCookieStore(this))); //使用sp保持cookie,如果cookie不过期,则一直有效

builder.cookieJar(new CookieJarImpl(new DBCookieStore(this)));//使用数据库保持cookie,如果cookie不过期,则一直有效

//builder.cookieJar(new CookieJarImpl(new MemoryCookieStore())); //使用内存保持cookie,app退出后,cookie消失

//设置请求头

HttpHeaders headers = new HttpHeaders();

headers.put("commonHeaderKey1", "commonHeaderValue1"); //header不支持中文,不允许有特殊字符

headers.put("commonHeaderKey2", "commonHeaderValue2");

//设置请求参数

HttpParams params = new HttpParams();

params.put("commonParamsKey1", "commonParamsValue1"); //param支持中文,直接传,不要自己编码

params.put("commonParamsKey2", "这里支持中文参数");

OkGo.getInstance().init(this) //必须调用初始化

.setOkHttpClient(builder.build()) //建议设置OkHttpClient,不设置会使用默认的

.setCacheMode(CacheMode.NO_CACHE) //全局统一缓存模式,默认不使用缓存,可以不传

.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE) //全局统一缓存时间,默认永不过期,可以不传

.setRetryCount(3) //全局统一超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0

.addCommonHeaders(headers) //全局公共头

.addCommonParams(params);

}

}

Post请求 使用的是json形式传参

Map map = new HashMap<>();

map.put("pageNum", 1);

map.put("pageSize", 10);

Gson gson = new Gson();

String json = gson.toJson(map);

OkGo.post(url) //输入请求地址

.tag(this)//销毁请求时候会用到

.upJson(json)//上传参数 如果没什么需求直接用 .params("key", "paramValue1")

.execute(new StringCallback() {

@Override

public void onSuccess(Response response) {

String body = response.body();

Gson bodyJson = new Gson();

MyBean myBean = bodyJson.fromJson(body, MyBean.class);

Log.d("MainActivity", "============" + myBean.getMsg());

Log.d("MainActivity", "============" + myBean.getData().getList().size());

}

@Override

public void onError(Response response) {

super.onError(response);

}

});

Get请求

OkGo.get(url)

.tag(this)//

.headers("header1", "headerValue1")//

.params("param1", "paramValue1")//

.execute(new StringCallback() {

@Override

public void onSuccess(Response response) {

//注意这里已经是在主线程了

String data = response.body();//这个就是返回来的结果

}

@Override

public void onError(Response response) {

super.onError(response);

}

});

文件上传

OkGo.post(url)

.tag(this)//

.headers("header1", "headerValue1")//

.headers("header2", "headerValue2")//

.params("param1", "paramValue1")//

.params("param2", "paramValue2")//

// .params("file1",new File("文件路径")) //这种方式为一个key,对应一个文件

// .params("file2",new File("文件路径"))

// .params("file3",new File("文件路径"))

.addFileParams("filename", files) // 这种方式为同一个key,上传多个文件

.execute(new JsonCallback() {

@Override

public void onStart(Request request) {

btnFormUpload.setText("正在上传中...");

}

@Override

public void onSuccess(Response response) {

btnFormUpload.setText("上传完成"+response.body());

}

@Override

public void onError(Response response) {

btnFormUpload.setText("上传出错");

}

@Override

public void uploadProgress(Progress progress) {

System.out.println("uploadProgress: " + progress);

}

});

文件下载

OkGo.get(url)

.tag(this)//

.headers("header1", "headerValue1")//

.params("param1", "paramValue1")//

.execute(new FileCallback("OkGo.apk") {

@Override

public void onStart(Request request) {

btnFileDownload.setText("正在下载中");

}

@Override

public void onSuccess(Response response) {

btnFileDownload.setText("下载完成"+response.body());

}

@Override

public void onError(Response response) {

btnFileDownload.setText("下载出错");

}

@Override

public void downloadProgress(Progress progress) {

System.out.println(progress);

}

});

请求销毁

@Override

protected void onDestroy() {

super.onDestroy();

//取消指定tag的请求

OkGo.getInstance().cancelTag(this);

//取消全部请求

OkGo.getInstance().cancelAll();

//取消OkHttpClient的全部请求

OkGo.cancelAll(new OkHttpClient());

OkGo.cancelTag(new OkHttpClient(), "且指定tag");

}

基本上就这些了,欢迎大家补充和指点

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441588.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 334B】Eight Point Sets(水题模拟,有坑)

题干&#xff1a; Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, ex…

一加6怎么刷android p6,一加6秒速跟进安卓P 教你尝鲜速成开发者

今年5月份&#xff0c;谷歌在I/O开发者大会上发布了全新的Android P操作系统&#xff0c;而在Android P系统发布后的没多久&#xff0c;一加手机官方就公开承诺&#xff0c;年度旗舰一加手机6将会成为首批次升级谷歌Android P系统的机型。而现在&#xff0c;一加已经开始兑现此…

*【CodeForces - 1047A】Little C Loves 3 I (水题,构造,三元组问题)

题干&#xff1a; Little C loves number 3 very much. He loves all things about it. Now he has a positive integer nn. He wants to split nn into 33 positive integers a,b,ca,b,c, such that abcnabcn and none of the 33 integers is a multiple of 33. Help him to…

html语言音乐添加路径,HTML5简单实现添加背景音乐的几种方法

这里推荐两种方法&#xff0c;就是两个标签 或者常用 css布局 隐藏播放器 做网站比较实用&#xff01;html5添加音乐说明&#xff1a;1、src毫无疑问写路径.2、使用hidden"true"表示隐藏音乐播放按钮&#xff0c;相反使用hidden"false"表示开启音乐播放按钮…

【CodeForces - 1047B 】Cover Points (数学,构造,思维)

题干&#xff1a; There are nn points on the plane, (x1,y1),(x2,y2),…,(xn,yn)(x1,y1),(x2,y2),…,(xn,yn). You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle …

html字段隐藏,如何刮取动态隐藏的HTML字段(UuViewState)值?

我在代码中完全实现了请求&#xff0c;但在FormRequest中意识到VIEWSTATE和EVENTVALIDATION是动态的&#xff0c;它们随每个请求而变化。我想做的是刮去它们以便在下一个请求中提供它们。在这两个字段都在HTML代码中提供&#xff0c;但隐藏了&lt&#xff1b;the __VIEWSTAT…

【CodeForces - 1051A】Vasya And Password (构造,水题)

题干&#xff1a; Vasya came up with a password to register for EatForces — a string ss. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its u…

html5引擎笔试题,最新!HTML5经典面试题型(附答案)

HTML已更新至HTML5&#xff0c;那么HTML5的测试题您也应该知道&#xff0c;这篇文章可以作为您的参考。1.doctype有什么作用呢&#xff1f;如何区分其混合模式和标准模式&#xff1f;所有这些都意味着什么&#xff1f;Doctype的作用是告诉浏览器使用HTML规范的哪个版本来渲染文…

【CodeForces - 1051B】Relatively Prime Pairs (构造,思维,素数,水题)

题干&#xff1a; You are given a set of all integers from ll to rr inclusive, l<rl<r, (r−l1)≤3⋅105(r−l1)≤3⋅105and (r−l)(r−l) is always odd. You want to split these numbers into exactly r−l12r−l12 pairs in such a way that for each pair (i,…

html 弹出加载页面,magnific popup:将整个html页面加载到弹出窗口中

我想用弹出的插件在弹出窗口中加载一个完整的html页面。如果我尝试&#xff1a;Edit images$(#edit-images-btn).magnificPopup({type: ajax});它产生了这个&#xff1a;这在图形上非常符合我的要求&#xff0c;但问题是的内容直接插入到dom中&#xff0c;而不是放在保护性的if…

【CodeForces - 1042A】Benches (优先队列,思维模拟,maxmin问题)

题干&#xff1a; There are nn benches in the Berland Central park. It is known that aiai people are currently sitting on the ii-th bench. Another mm people are coming to the park and each of them is going to have a seat on some bench out of nn available. …

网页html 图片横向摆放,css实现多张图片横向居中显示的方法

先讲一下实现的步骤&#xff1a;最终效果2. 代码实现HTML部分分类小贴士CSS部分.main{width:100%;margin-top:40px;}.main .tag{margin:0 auto;width:200px;font-size:18px;border-bottom:1px solid #878787;text-align:center;margin-bottom:20px;}.main .images{margin:0 aut…

【CodeForces - 1042B】Vitamins(去重方法,二进制或stlmap,水题)

题干&#xff1a; Berland shop sells nn kinds of juices. Each juice has its price cici. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice ca…

android 中断处理流程,Android P的native crash处理流程

一、概述Android系统有监控程序异常退出的机制&#xff0c;这便是本文要讲述得debuggerd守护进程。当发生native crash或者主动调用debuggerd时&#xff0c;会输出进程相关的状态信息到文件或者控制台。输出的debuggerd数据 保存在文件/data/tombstones/tombstone_XX&#xff0…

数论中的无数公式 总结

斯特林公式是一条用来取n阶乘近似值的数学公式。一般来说&#xff0c;当n很大的时候&#xff0c;n阶乘的计算量十分大&#xff0c;所以斯特灵公式十分好用&#xff0c;而且&#xff0c;即使在 n很小的时候&#xff0c;斯特灵公式的取值已经十分准确。 公式为&#xff1a; 以下…

计算机怎样连接网络适配器,win10 本地连接共享internet时, 会将承载网络适配器IP设置成192.168.137.1但不成功...

您好&#xff0c;谢谢您&#xff01;我后来参考了http://www.ishanarora.com/2009/07/29/windows-7-as-a-wireless-access-point/ 中的 Jenny提到的方案COMPLETE SOLUTION:### create virtual vwlan hosted port, set AP ssid to advertise, and its password- [cmd] netsh wla…

【POJ - 1001 】Exponentiation (Java大数,高精度)

题干&#xff1a; Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write…

用计算机唱出惊雷,除了《惊雷》还有多少喊麦神曲?这十首神作你一定听过!...

一首《惊雷》可以说是火遍了大江南北&#xff0c;一时间风头无二。而这两天杨坤对《惊雷》的diss、惊雷原唱六道的回应更是成为全网热搜&#xff0c;这也让以《惊雷》为首的“喊麦文化”再次进入了公众的视线。事实上除了《惊雷》&#xff0c;还有不少脍炙人口的喊麦神曲&#…

ACM算法 -- 数论 -- 开灯关灯问题(数论,整数分解,因子个数,公式推导)

有编号1~100个灯泡&#xff0c;起初所有的灯都是灭的。有100个同学来按灯泡开关&#xff0c;如果灯是亮的&#xff0c;那么按过开关之后&#xff0c;灯会灭掉。如果灯是灭的&#xff0c;按过开关之后灯会亮。 现在开始按开关。 第1个同学&#xff0c;把所有的灯泡开关都按一次…

计算机编程是考研什么专业,程序员考研该不该继续选择计算机专业

首先&#xff0c;近些年来确实有不少程序员会选择通过读研来突破自身的岗位发展瓶颈&#xff0c;大部分程序员在读研后也都获得了岗位升级&#xff0c;还有一部分程序员会继续读博&#xff0c;从而进入到科研和教育领域发展。从当前的技术发展趋势和人才需求趋势来看&#xff0…