retrofit网络请求框架
来源:互联网 发布:国研网数据子库 编辑:程序博客网 时间:2024/05/01 19:50
- 文章来自Retrofit20使用详解
- 直接请求JOSN
- 通过依赖retrofit-converter-gson直接返回实体类
文章来自Retrofit2.0使用详解
android studio可引用gradle compile 集成retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
直接请求JOSN
首先创建一个interface请求接口
注解对应的包如下
import retrofit2.http.GET;import retrofit2.http.Path;
以下请求接口
interface GitHubApi{ //owner:是用户路径 //repo:是响应地址 //很显然,这是多变的,不同有不同的路径 //路径注入的方式,这种方式是get请求时,有需要多种变换 @GET("repos/{owner}/{repo}/contributors") Call<ResponseBody> contributorsBySimpleGetCall(@Path("owner") String own,@Path("repo") String repo); }
上面这段代码,@GET()注解中,有用{},对应参数上注解@Path。
下面调用代码
//实例retrofit,retrofit采用的是builder模式 Retrofit retrofit=new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); //调用create,让retrofit去实现我们所创建的interfaceGitHubApi repo=retrofit.create(GitHubApi.class); Call<ResponseBody> responseBodyCall = repo.contributorsBySimpleGetCall("square","retrofit"); Retrofit retrofit=new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); GitHubApi repo=retrofit.create(GitHubApi.class); Call<ResponseBody> responseBodyCall = repo.contributorsBySimpleGetCall("square","retrofit"); //请求网络,并返回response responseBodyCall.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { Log.i("contributor","是请求后的响应结果"+response.body().string()); Gson gson=new Gson(); ArrayList<Contributor> con=gson.fromJson(response.body().string(),new TypeToken<List<Contributor>>(){}.getType()); if (con!=null){ for (Contributor contributor:con){ Log.i("contributor",contributor.getLogin()); } }else{ Log.i("contributor","con为空"); } } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Log.e("contributor","请求失败"); } });
通过依赖retrofit-converter-gson直接返回实体类
android studio首先需要再增加一个依赖
//这个依赖,是retrofit集成了gson,可用于直接转换为实体类,无需开发者解析compile 'com.squareup.retrofit2:converter-gson:2.0.1'
square提供了多种解析json的依赖库
Gson: compile 'com.squareup.retrofit2:converter-gson:2.0.1'Jackson: compile 'com.squareup.retrofit2:converter-jackson:2.0.1'Moshi: compile 'com.squareup.retrofit2:converter-moshi:2.0.1'Protobuf: compile 'com.squareup.retrofit2:converter-protobuf:2.0.1'Wire: compile 'com.squareup.retrofit2:converter-wire:2.0.1'Simple XML: compile 'com.squareup.retrofit2:converter-simplexml:2.0.1'Scalars (primitives, boxed, and String): compile 'com.squareup.retrofit2:converter-scalars:2.0.1'
修改接口定义
//需要添加依赖 compile 'com.squareup.retrofit2:converter-gson:2.0.1', //这是retrofit的转换器,内部集成了gson //自动解析json将其转换为实体类 @GET("repos/{owner}/{repo}/contributors") Call<List<Contributor>> getContributors(@Path("owner") String own,@Path("repo") String repo);
可以看到有一点不同,就是返回值中的泛型定义直接改为实体类集合
Retrofit的build增加了GSON转换器
Retrofit retrofit=new Retrofit.Builder() .baseUrl("https://api.github.com/") //添加gson转换器,直接转换为实体类 .addConverterFactory(GsonConverterFactory.create()) .build();
实际调用展示
Retrofit retrofit=new Retrofit.Builder() .baseUrl("https://api.github.com/") //添加gson转换器,直接转换为实体类 .addConverterFactory(GsonConverterFactory.create()) .build(); GitHubApi repo=retrofit.create(GitHubApi.class); Call<List<Contributor>> responseBodyCall = repo.getContributors("square","retrofit"); responseBodyCall.enqueue(new Callback<List<Contributor>>() { @Override public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) { Gson gson=new Gson(); if (response.body()!=null&&response.body().size()>0){ for (Contributor contributor:response.body()){ Log.i("contributor",contributor.getLogin()); } }else{ Log.i("contributor","list为空"); } } @Override public void onFailure(Call<List<Contributor>> call, Throwable t) { Log.e("contributor","请求失败"); } });
0 0
- retrofit网络请求框架
- 网络请求框架:Retrofit
- 网络请求框架Retrofit
- Retrofit网络请求框架
- Retrofit网络请求框架,简介
- Retrofit+okhttp网络框架请求
- Retrofit+RxJava网络请求框架
- Retrofit网络请求框架要点总结
- Android网络请求框架之Retrofit(一)
- Android网络请求框架之Retrofit(二)
- Android网络请求框架之Retrofit(三)
- Android高效网络请求Retrofit框架
- Android网络请求框架之Retrofit实践
- Retrofit网络请求框架基础操作
- Android网络请求框架Retrofit使用详解
- 网络请求框架Retrofit的基本使用
- Android 网络请求框架之Rxjava+Retrofit
- Android 网络请求框架之Rxjava+Retrofit
- HTML+CSS基础 table中的tr中的td的colspan属性创建跨列的单元格(创建不规整表格)
- 软链接和硬链接
- Linq常用语法
- insertBefore() 方法
- iPhone 7与7 Plus 相机区别有多大 请看测试
- retrofit网络请求框架
- CSS基础(七):z-index详解
- WebCrawler Java小爬爬 从入门到放弃 第四章
- TCP三次握手和四次挥手的全过程
- ……
- 状态模式
- (SpingMVC+Mybatis)前台页面checkbox的传值与回写,下拉框option的传值与回写。
- HDU5904【瞎搞】
- HTML+CSS基础 table中的tr中的td的rowspan属性创建跨行的单元格(创建不规整表格)