网络框架——Retrofit

Retrofit的注解分类

Retrofit的注解分为三大类,分别是:HTTP请求方法注解、标记类注解、参数类注解

 

GET请求

首先创建爱你请求地址和返回参数类型

public interface IpService
{
    @GET("getIpInfo.php?ip=59.108.54.37")
    Call getIpMsg();
}

创建请求

private void getIp()
    {
        String url = "http://ip.taobao.com/service/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())//增加返回值Json支持
                .build();
        IpService ipService = retrofit.create(IpService.class);
        Call call = ipService.getIpMsg();
        call.enqueue(new Callback()
        {
            @Override
            public void onResponse(Call call, Response response)
            {
                String country = response.body().getData().getCountry();
                Log.e("得到的请求返回", country);
            }

            @Override
            public void onFailure(Call call, Throwable t)
            {

            }
        });
    }

Retrofit也可以使用动态链接

GET 动态链接请求

public interface IpSercieceForPath
{
    @GET("{path}/getIpInfo.php?ip=59.108.54.37")
    Call getIpMsg(@Path("path") String path);//动态链接url
}
 /**
     * 动态URL
     */
    private void getIp2()
    {
        String url = "http://ip.taobao.com/service/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())//增加返回值Json支持
                .build();
        IpSercieceForPath ipService = retrofit.create(IpSercieceForPath.class);
        Call call = ipService.getIpMsg("http://ip.taobao.com/service");//动态链接
        call.enqueue(new Callback()
        {
            @Override
            public void onResponse(Call call, Response response)
            {
                String country = response.body().getData().getCountry();
                Log.e("得到的请求返回", country);
            }

            @Override
            public void onFailure(Call call, Throwable t)
            {
                Log.e("得到的请求返回", t.getMessage());
            }
        });
    }

.@Query

可以动态指定参数的值


@GET("getIpInfo.php?")
    Call getIp(@Query("ip") String ip);//拼接ip  后面的是正式数值

@QueryMap

为了更加精确的查找我们需要的数据,需要传入很多查询数据,所以可以将所有参数继承在一个Map中统一传递

    @GET("getIpInfo.php?")
    Call getIpMsgMap(@QueryMap Map options);//拼接ip 活其他数值

POST 访问请求

@FormUrlEncoded  //注明为表单请求
    @POST("getIpInfo.php?")
    Call getIpMsg(@Field("ip") String first);  //Field后面为一对键值

@BODY 将整个Json作为一个body进行传递

 @POST("getIpInfo.php?")
    Call getMsg(@Body IpModel ip);//传递表单,将整个类多为一个体传递

请求时需要下列写法

 IpService ipService = retrofit.create(IpService.class);
        Call call = ipService.getMsg(new IpModel());

@PART单文件上传

 @Multipart//表示可以多个Part
    @POST("user/photo")
    Call updateModel(@Part MultipartBody.Part photo, @Part("description") RequestBody requestBody);//第一个参数是准备上传的文件,另一个参数是用来传递简单的键值对
 IpService ipService = retrofit.create(IpService.class);
        File file = new File(Environment.getExternalStorageDirectory(), "wa.png");
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
        MultipartBody.Part photo = MultipartBody.Part.createFormData("photos", "wanshu.png", requestBody);
        Call call = ipService.updateModel(photo, RequestBody.create(null, "wa"));

@Header

为了防止攻击和过滤不安全的访问所以使用消息头

你可能感兴趣的:(网络请求)