Retrofit2的简单使用

Retrofit是网络请求库,适用于Android和Java的类型安全HTTP客户端。

本文以淘宝IP地址库为测试地址

网络权限

<uses-permission android:name="android.permission.INTERNET"/>

依赖

implementation 'com.squareup.retrofit2:retrofit:2.9.0'

1、简单使用

1.1.创建Retrofit实例

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://ip.taobao.com/")
        .build();

1.2.创建接口

public interface TaobaoIpService {
    @GET("outGetIpInfo")
    Call<ResponseBody> getIP(@Query("ip") String ip);
}

支持的注解

1.3.接口调用

        Call<ResponseBody> call = service.getIP("192.168.1.1");
        //异步请求
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Log.i(TAG, "onResponse: " + response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });

1.4.输出结果

onResponse: {"data":{"area":"","country":"XX","isp_id":"local","queryIp":"192.168.1.1","city":"内网IP","ip":"192.168.1.1","isp":"内网IP","county":"","region_id":"xx","area_id":"","county_id":null,"region":"XX","country_id":"xx","city_id":"local"},"msg":"query success","code":0}

2、使用Gson作为Converter 

2.1添加依赖

    implementation 'com.google.code.gson:gson:2.8.8'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

2.2创建自定义Gson

        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd hh:mm:ss")
                .create();

2.3创建Retrofit实例

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://ip.taobao.com")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

2.4创建要转换的模型

public class Result {

    private Data data;
    private String msg;
    private int code;

    getter()
    setter()

    public static class Data {
        private String area;
        private String country;
        private String isp_id;
        private String queryIp;
        private String city;
        private String ip;
        private String isp;
        private String county;
        private String region_id;
        private String area_id;
        private Object county_id;
        private String region;
        private String country_id;
        private String city_id;

        getter()
        setter()

        @Override
        public String toString() {
            return "Data{" +
                    "area='" + area + '\'' +
                    ", country='" + country + '\'' +
                    ", isp_id='" + isp_id + '\'' +
                    ", queryIp='" + queryIp + '\'' +
                    ", city='" + city + '\'' +
                    ", ip='" + ip + '\'' +
                    ", isp='" + isp + '\'' +
                    ", county='" + county + '\'' +
                    ", region_id='" + region_id + '\'' +
                    ", area_id='" + area_id + '\'' +
                    ", county_id=" + county_id +
                    ", region='" + region + '\'' +
                    ", country_id='" + country_id + '\'' +
                    ", city_id='" + city_id + '\'' +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "Result{" +
                "data=" + data +
                ", msg='" + msg + '\'' +
                ", code=" + code +
                '}';
    }
}

2.5创建接口

public interface TaobaoIpService {
    @GET("outGetIpInfo")
    Call<Result> getIP(@Query("ip") String ip);
}

 2.6接口调用

        TaobaoIpService service = retrofit.create(TaobaoIpService.class);
        Call<Result> call = service.getIP("192.168.1.1");
        //异步请求
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                Log.i(TAG, response.body().toString());
                Log.i(TAG, "msg=" + response.body().getMsg());
            }

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

            }
        });

2.7返回结果

Result{data=Data{area='', country='XX', isp_id='local', queryIp='192.168.1.1', city='内网IP', ip='192.168.1.1', isp='内网IP', county='', region_id='xx', area_id='', county_id=null, region='XX', country_id='xx', city_id='local'}, msg='query success', code=0}

msg=query success

通过设置Converter很容易获取值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值