G相关文章

Android网络编程(一)HTTP协议原理

Android网络编程(二)HttpClient与HttpURLConnection

Android网络编程(三)Volley用法全解析

Android网络编程(四)从源码解析volley

Android网络编程(五)OkHttp2.x用法全解析

Android网络编程(六)OkHttp3用法全解析

Android网络编程(七)源码解析OkHttp前篇[请求网络]

Android网络编程(八)源码解析OkHttp后篇[复用连接池]

Android网络编程(九)Retrofit2前篇[基本使用]

前言

在上一篇Android网络编程(九)Retrofit2前篇[基本使用]中我们了解了Retrofit的最基本的GET方式访问网络的写法以及请求参数的简单介绍。这一篇我们来详细的了解Retrofit的注解。

1.GET请求访问网络

动态配置URL地址:@Path

Retrofit提供了很多的请求参数注解,使得请求网路时更加便捷。在这里我们仍旧访问淘宝ip库。其中,@Path用来动态的配置URL地址。请求网络接口代码如下所示。

public interface IpServiceForPath {
@GET("{path}/getIpInfo.php?ip=59.108.54.37")
Call<IpModel> getIpMsg(@Path("path") String path);
}

在GET注解中包含了{path},它对应着@Path注解中的”path”,而用来替换{path}的正是需要传入的 “String path”的值。接下来请求网络的代码如下所示。

String url = "http://ip.taobao.com/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
IpServiceForPath ipService = retrofit.create(IpServiceForPath.class);
Call<IpModel>call=ipService.getIpMsg("service");//1
call.enqueue(new Callback<IpModel>() {
@Override
public void onResponse(Call<IpModel> call, Response<IpModel> response) {
String country= response.body().getData().getCountry();
Toast.makeText(getApplicationContext(),country,Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<IpModel> call, Throwable t) {
}
});

在注释1处,传入”service”来替换 @GET注解中的{path}的值。

动态指定查询条件:@Query与@QueryMap

在上一篇中我们用@Query来动态的替换ip地址为了能更方便的得到该ip所对应的地理信息:

public interface IpServiceForQuery{
@GET("getIpInfo.php")
Call<IpModel> getIpMsg(@Query("ip")String ip);
}

但是在网络请求中一般为了更精确的查找到我们所需要的数据,需要传入很多的查询参数,如果用@Query会比较麻烦,这时我们可以采用@QueryMap,将所有的参数集成在一个Map统一传递:

public interface IpServiceForQueryMap {
@GET("getIpInfo.php")
Call<IpModel> getIpMsg(@QueryMap Map<String, String> options);
}

2.POST请求访问网络

传输数据类型为键值对:@Field

传输数据类型为键值对,这是我们最常用的POST请求数据类型,淘宝ip库支持数据类型为键值对的POST请求:

public interface IpServiceForPost {
@FormUrlEncoded
@POST("getIpInfo.php")
Call<IpModel> getIpMsg(@Field("ip") String first);
}

首先用到@FormUrlEncoded注解来标明这是一个表单请求,然后在getIpMsg方法中使用@Field注解来标示所对应的String类型数据的键,从而组成一组键值对进行传递。接下来请求网络的代码如下所示。

 String url = "http://ip.taobao.com/service/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
IpServiceForPost ipService = retrofit.create(IpServiceForPost.class);
Call<IpModel>call=ipService.getIpMsg("59.108.54.37");
call.enqueue(new Callback<IpModel>() {
@Override
public void onResponse(Call<IpModel> call, Response<IpModel> response) {
String country= response.body().getData().getCountry();
Toast.makeText(getApplicationContext(),country,Toast.LENGTH_SHORT).show();
} @Override
public void onFailure(Call<IpModel> call, Throwable t) {
}
});

传输数据类型Json字符串:@Body

我们也可以用POST方式将Json字符串作为请求体发送到服务器,请求网络接口代码为:

public interface IpServiceForPostBody {
@POST("getIpInfo.php")
Call<IpModel> getIpMsg(@Body Ip ip);
}

用@Body这个注解标识参数对象即可,retrofit会将Ip对象转换为字符串。

public class Ip {
private String ip;
public Ip(String ip) {
this.ip = ip;
}
}

请求网络的代码基本上都是一致的:

 String url = "http://ip.taobao.com/service/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
IpServiceForPostBody ipService = retrofit.create(IpServiceForPostBody.class);
Call<IpModel>call=ipService.getIpMsg(new Ip(ip));
call.enqueue(new Callback<IpModel>() {
@Override
public void onResponse(Call<IpModel> call, Response<IpModel> response) {
String country= response.body().getData().getCountry();
Log.i("wangshu","country"+country);
Toast.makeText(getApplicationContext(),country,Toast.LENGTH_SHORT).show();
} @Override
public void onFailure(Call<IpModel> call, Throwable t) { }
});
}

运行程序用Fiddler抓包,如下图所示。

可以看到请求数据是一个Json字符串,因为淘宝ip库并不支持此类型所以不会返回我们需要的地理信息数据。

单个文件上传:@Part

public interface UploadFileForPart {
@Multipart
@POST("user/photo")
Call<User> updateUser(@Part MultipartBody.Part photo, @Part("description") RequestBody description);
}

Multipart注解表示允许多个@Part,updateUser方法第一个参数是准备上传的图片文件,使用了MultipartBody.Part类型,另一个参数是RequestBody类型,它用来传递简单的键值对。请求网络代码如下所示。

...
File file = new File(Environment.getExternalStorageDirectory(), "wangshu.png");
RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/png"), file);
MultipartBody.Part photo = MultipartBody.Part.createFormData("photos", "wangshu.png", photoRequestBody);
UploadFileForPart uploadFile = retrofit.create(UploadFileForPart.class);
Call<User> call = uploadFile.updateUser(photo, RequestBody.create(null, "wangshu"));
...

多个文件上传:@PartMap

@Multipart
@POST("user/photo")
Call<User> updateUser(@PartMap Map<String, RequestBody> photos, @Part("description") RequestBody description);

和单文件上传是类似的,只是使用Map封装了上传的文件,并用@PartMap注解来标示起来。其他的都一样,这里就不赘述了。

3.消息报头Header

Http请求中,为了防止攻击或是过滤掉不安全的访问或是添加特殊加密的访问等等,用来减轻服务器的压力和保证请求的安全,通常都会在消息报头中携带一些特殊的消息头处理。Retrofit也提供了@Header来添加消息报头。添加消息报头有两种方式,一种是静态的,另一种是动态的,先来看静态的方式,如下所示。

interface SomeService {
@GET("some/endpoint")
@Headers("Accept-Encoding: application/json")
Call<ResponseBody> getCarType();
}

使用@Headers注解添加消息报头,如果想要添加多个消息报头,则可以使用{}包含起来:

interface SomeService {
@GET("some/endpoint")
@Headers({
"Accept-Encoding: application/json",
"User-Agent: MoonRetrofit"
})
Call<ResponseBody> getCarType();
}

动态的方式添加消息报头如下所示。

interface SomeService {
@GET("some/endpoint")
Call<ResponseBody> getCarType(
@Header("Location") String location);
}

使用@Header注解,可以通过调用getCarType方法来动态的添加消息报头。

github源码下载


欢迎关注我的微信公众号,第一时间获得博客更新提醒,以及更多成体系的Android相关原创技术干货。

扫一扫下方二维码或者长按识别二维码,即可关注。

最新文章

  1. PPPoE名词解释
  2. Java并发编程实现概览
  3. Chap5: question: 29 - 31
  4. LightOJ1348 树链剖分
  5. 恢复被win7覆盖的Ubuntu Grub
  6. linux驱动系列之s3c2440内存布局
  7. QT的信号与槽机制介绍
  8. 转移服务器,DEDE网站遇到no input file specified!
  9. 【Android UI设计与开发】第17期:滑动菜单栏(二)开源项目SlidingMenu的示例
  10. 【WebSocket初探 】
  11. 移动App开发基本技术面
  12. Docker 教程(一)
  13. win10升级后蓝牙不见了,设备管理器里没有,多了个串行控制器里的未知USB设备?
  14. P2089 烤鸡(搜索简单题)
  15. 关于SpringBoot如何返回视图
  16. FMDB基本操作
  17. android 点击通知栏返回应用 ,非启动一个新Activity
  18. 调用WebAPI接口地址返回序列化的JSON对象中的属性都加了个k__BackingField关键字的解决办法
  19. [label][Smarty]Smarty使用心得
  20. vue打包体积优化之旅

热门文章

  1. HTTP缓存和CDN缓存
  2. badboy提示脚本错误解决方法
  3. [ASP.Net] 转 &gt; ASP.NET MVC 小牛之路
  4. caffe 参数介绍 solver.prototxt
  5. react拼接class&amp;将JS标签转换为HTML
  6. 洛谷P2181 对角线(组合数)
  7. angular中的ng-click指令案例
  8. APP开发中,如何从UI设计上提升APP用户体验
  9. 让Kafka在scala里面跑起来
  10. (转)基于MVC4+EasyUI的Web开发框架形成之旅--权限控制