Retrofit的简单使用
来源:互联网 发布:组策略 禁止软件运行 编辑:程序博客网 时间:2024/05/16 14:24
一、概念
(1)gradle下导入正确的库,由于依赖于OkHttp,所以还有导入这个库
(2)定义一个接口,这个接口是用来规定请求参数的样式及提供请求参数
(3)创建Retrofit对象,通过对象和接口来创建请求队列并得到结果
1. 添加依赖
compile 'com.squareup.retrofit2:retrofit:2.1.0'compile 'com.squareup.retrofit2:converter-gson:2.1.0'
2. 创建接口
public interface APi { @Headers("apikey:81bf9da930c7f9825a3c3383f1d8d766") @GET("word/word") Call<News> getNews(@Query("num") String num,@Query("page")String page);}
3. 创建对象
Retrofit retrofit = new Retrofit.Builder() //使用自定义的mGsonConverterFactory .addConverterFactory(GsonConverterFactory.create()) .baseUrl("http://apis.baidu.com/txapi/") .build();mApi = retrofit.create(APi.class);
4. 请求
mApi = retrofit.create(APi.class);Call<News> news = mApi.getNews("1", "10");news.enqueue(new Callback<News>() { @Override public void onResponse(Call<News> call, Response<News> response) { } @Override public void onFailure(Call<News> call, Throwable t) { }});以上就是简单使用的过程
四、请求方式
主要有一下几种请求方法
各种请求注解的意思
加入我们想请求这样的网址:http://apis.baidu.com/txapi/world/world?num=10&page=1,header为"apikey:81bf9da930c7f9825a3c3383f1d8d766"
我们可以这样请求:
(1)接口这样定义
@Headers("apikey:81bf9da930c7f9825a3c3383f1d8d766") @GET("word/word") Call<News> getNews(@Query("num") String num,@Query("page")String page);
//创建retrofit对象Retrofit retrofit = new Retrofit.Builder() //使用自定义的mGsonConverterFactory .addConverterFactory(GsonConverterFactory.create()) .baseUrl("http://apis.baidu.com/txapi/") .build();// 实例化我们的mApi对象mApi = retrofit.create(APi.class); // 调用我们的响应的方法Call<News> news = mApi.getNews(number, page);news.enqueue(new Callback<News>() { @Override public void onResponse(Call<News> call, Response<News> response) { News body = response.body(); Logger.i("onResponse: ="+body.toString()); } @Override public void onFailure(Call<News> call, Throwable t) { Logger.i("onResponse: ="+t.getMessage()); }});
假设BaseUrl是http://apis.baidu.com/txapi/的前提下
(1)其中 @GET("word/word")会追加到baseUrl :http://apis.baidu.com/txapi/的后面,即变成:http://apis.baidu.com/txapi/world/world
(2)@Query("num") String num,@Query("page")String page;分别对应键值的名称与值。会追加到http://apis.baidu.com/txapi/world/world的后面,请求网址即变成:http://apis.baidu.com/txapi/world/world?num=10&page=1
(3) @Headers("apikey:81bf9da930c7f9825a3c3383f1d8d766")是 在基础之上为 其添加响应头
(4)如果想继续增加参数,只需要在方法参数追加这样的形式就OK了:@Query("page")String page
@Headers("apikey:81bf9da930c7f9825a3c3383f1d8d766") @GET("word/word") Call<News> getNews(@Query("num") String num,@Query("page")String page,@Query("type") String type);
(5)加入我们想要请求这样的网址http://apis.baidu.com/txapi/tiyu/tiyu?num=10&page=1,,我们可以这样写 @Headers({"apikey:81bf9da930c7f9825a3c3383f1d8d766" ,"Content-Type:application/json"}) @GET("{type}/{type}") Call<News> tiYu(@Path("type") String type, @Query("num") String num,@Query("page")String page); String type="tiyu"; Call<News> news = api.tiYu(type,number, page);
六、Post请求
@FormUrlEncoded@Headers({"apikey:81bf9da930c7f9825a3c3383f1d8d766" ,"Content-Type:application/json"})@POST("world/world")Call<News> postNews(@Field("num") String num, @Field("page")String page);
Header是添加请求头(2)通过post提交Json数据
有时提交的数据量比较大时,用键值对的方式提交参数不太方便,Retrofit可以通过@Body注释,直接传递一个对象给请求主体,Retrofit通过JSON转化器,把对象映射成JSON数据。
假设我们需要提交的数据为
{ "id": 1, "text": "my task title"}
接口定义如下
public interface TaskService { @Headers({"Content-Type: application/json","Accept: application/json"}) @POST("/tasks") Call<Task> createTask(@Body Task task);}
实体的Model如下
public class Task { private long id; private String text; public Task() {} public Task(long id, String text) { this.id = id; this.text = text; }}
客户端调用
Task task = new Task(1, "my task title"); Call<Task> call = taskService.createTask(task); call.enqueue(new Callback<Task>() {});
最终结果如下
{ "id": 1, "text": "my task title"}
尊重作者,尊重原创,参考文章:
原作者将的更加的详细,请参看
http://www.jianshu.com/p/82f8b58db53c
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- retrofit的简单使用
- retrofit的简单使用
- Retrofit 的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Retrofit的简单使用
- Oracle初始化参数文件spfile 和 pfile
- 使用freetype库将文字嵌入到bmp图片中 学习笔记
- 关于plsqldeveloper打开报错解决方法MSVCR71.dll is missing from your compute
- ZIP数据压缩/解压工具
- 自定义可拖拽显示数字BadgeView(仿QQ可拖拽控件)
- Retrofit的简单使用
- iOS开发资源
- 【南阳OJ 91】 阶乘之和(贪心)
- HDU 6035 colorful tree
- ios开发之duplicate symbol for architecture x86_64 错误
- 1011. A+B和C (15)
- adb命令总结
- Java设计模式学习09——装饰模式
- vue与vue-i18n结合实现后台数据的多语言切换