没有合适的资源?快使用搜索试试~ 我知道了~
Retrofit2+RxJava+Gson的使用1
需积分: 0 0 下载量 152 浏览量
2022-08-08
20:25:20
上传
评论
收藏 16KB DOCX 举报
温馨提示
试读
4页
使用步骤如下:使用这几个框架需要配置环境://rxjava compile 'io.reactivex.rxjava2:rxjava:2.0.1' compil
资源详情
资源评论
资源推荐
使用步骤如下:
1. 使用这几个框架需要配置环境:
//rxjava
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
2. 定义一个接口,写请求
public interface IUserBiz
{
@GET("users")
Call<List<User>> getUsers();
//可以更多
}
3. 完成请求:创建一个 retrofit,通过 retrofit 的 create 方法创建刚才写的请求接口,通过 Call
的 enqueue(异步)或者 execute(同步)进行网络请求
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.31.242:8080/springmvc_users/user/")
.addConverterFactory(GsonConverterFactory.create())
.build();
IUserBiz userBiz = retrofit.create(IUserBiz.class);
Call<List<User>> call = userBiz.getUsers();
call.enqueue(new Callback<List<User>>()
{
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response)
{
Log.e(TAG, "normalGet:" + response.body() + "");
}
@Override
public void onFailure(Call<List<User>> call, Throwable t)
{
}
});
小明斗
- 粉丝: 25
- 资源: 329
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0