Android网络编程(十)Retrofit2后篇[注解]

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相关原创技术干货。
扫一扫下方二维码或者长按识别二维码,即可关注。

posted @ 2016-11-02 16:14  挨踢人啊  阅读(282)  评论(0编辑  收藏  举报