Retrofit2的使用和封装
本篇文章包含以下内容:
Retrofit2是什么
Retrofit2工具类的演示(基于Retrofit2工具类的封装)
Retrofit2工具类的封装
Retrofit2是什么
使用项目的原话:Android和Java中类型安全的HTTP客户端
项目地址:https://github.com/square/retrofit
Retrofit2的基本使用
1、Get请求
2、Post请求
Retrofit2的导入
compile 'com.squareup.retrofit2:retrofit:2.1.0'
Retrofit2工具类的演示
访问接口是,该接口不能使用post方式
http://japi.juhe.cn/joke/content/list.from?key=488c65f3230c0280757b50686d1f1cd5&&sort=asc&&time=1418816972
返回数据
{
"error_code": 0,
"reason": "Success",
"result": { "data":[ { "content":"学校论坛上有人问:“为啥明明用了除蟑螂的药,蟑螂却越来越多了。”某个学生回帖:“如果你家人不见了,你不出来找吗?你会不着急么?”", "hashId":"8196907ee902f3508b9be6ea59d2191c", "unixtime":1478598830, "updatetime":"2016-11-08 17:53:50"} ]}}
Get请求演示
/*
* Get请求
* 参数已经封装在工具类的Url中
*/
Call call = RetrofitUtils.getInstance().get();
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
tv.setText(response.body().toString());
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
Map map = new HashMap<>();
map.put("key","488c65f3230c0280757b50686d1f1cd5");
map.put("sort","asc");
map.put("time&