Android网络请求开源框架retrofit的基本GET用法(2.4版本)

1.先在gradle配置retrfit版本如下:

  //rxjava/retrofit相关引入
    compile 'io.reactivex.rxjava2:rxandroid:2.0.2'
    compile 'io.reactivex.rxjava2:rxjava:2.1.12'
    compile 'com.squareup.retrofit2:retrofit:2.4.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'

2.写一个接口,配置网络请求地址参数等

public interface WalletService {
    /**
     * 获取优惠信息的接口
     * @return 返回值
     */
    @GET("/tools/mockapi/3191/favourable")//配置的get请求
    Call<String> getFavourable();
}

3.开启请求

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.wanandroid.com")
                .addConverterFactory(new Converter.Factory() {
                    @Nullable
                    @Override
                    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                        return new Converter<ResponseBody, String>() {
                            @Override
                            public String convert(ResponseBody value) throws IOException {
                                return value.string();
                            }
                        };
                    }
                })
                .build();

        WalletService service = retrofit.create(WalletService.class);
        Call<String> favourable = service.getFavourable();
        favourable.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String message = response.body();
                Log.e("打印返回的json数据",message);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                //请求失败的处理
            }
        });

4.看下完整的请求地址:

http://www.wanandroid.com/tools/mockapi/3191/favourable

5.这是用retorfit请求到的json数据:

{
    "data": [
        {
            "activity1": "美珍香满50减15",
            "activity2": "美珍香满45.5元购50元代金券",
            "bankType": "招行",
            "businessType": "生活|",
            "distance": "27m",
            "message": "美珍香(北京来福土店)",
            "money": "人均¥34",
            "payType": "银联"
        },
        {
            "activity1": "粗娘人家满100减35",
            "activity2": "粗娘人家80元购35元代金券",
            "bankType": "建行",
            "businessType": "美食|",
            "distance": "35m",
            "message": "粗娘人家(朝阳门店)",
            "money": "人均¥128",
            "payType": "银联"
        },
        {
            "activity1": "燕窝楼满200减55",
            "activity2": "燕窝楼135元购75元代金券",
            "bankType": "交行",
            "businessType": "美食|",
            "distance": "42m",
            "message": "燕窝楼满(朝阳门店)",
            "money": "人均¥98",
            "payType": "银联"
        },
        {
            "activity1": "味多美满70减15",
            "activity2": "味多美满25元购5元代金券",
            "bankType": "工行",
            "businessType": "面包蛋糕|",
            "distance": "47m",
            "message": "味多美满(朝阳门店)",
            "money": "人均¥78",
            "payType": "银联"
        },
        {
            "activity1": "良品铺子满50减15",
            "activity2": "良品铺子73元购27元代金券",
            "bankType": "农行",
            "businessType": "零食|",
            "distance": "52m",
            "message": "良品铺子(朝阳门店)",
            "money": "人均¥68",
            "payType": "银联"
        },
        {
            "activity1": "酸奶网满30减11",
            "activity2": "酸奶网26元购7元代金券",
            "bankType": "邮政",
            "businessType": "零食|",
            "distance": "55m",
            "message": "酸奶网(朝阳门店)",
            "money": "人均¥39",
            "payType": "银联"
        },
        {
            "activity1": "万龙州海鲜满79减31",
            "activity2": "万龙州海鲜298元购65元代金券",
            "bankType": "浦发",
            "businessType": "海鲜|",
            "distance": "63m",
            "message": "万龙州海鲜(朝阳门店)",
            "money": "人均¥197",
            "payType": "银联"
        },
        {
            "activity1": "抹茶奶满25减9",
            "activity2": "抹茶奶66元购13元代金券",
            "bankType": "平安",
            "businessType": "生活|",
            "distance": "69m",
            "message": "抹茶奶海鲜(朝阳门店)",
            "money": "人均¥23",
            "payType": "银联"
        },
        {
            "activity1": "燕窝楼满200减55",
            "activity2": "燕窝楼135元购75元代金券",
            "bankType": "交行",
            "businessType": "美食|",
            "distance": "76m",
            "message": "燕窝楼满(朝阳门店)",
            "money": "人均¥98",
            "payType": "银联"
        },
        {
            "activity1": "味多美满70减15",
            "activity2": "味多美满25元购5元代金券",
            "bankType": "工行",
            "businessType": "面包蛋糕|",
            "distance": "79m",
            "message": "味多美满(朝阳门店)",
            "money": "人均¥78",
            "payType": "银联"
        },
        {
            "activity1": "良品铺子满50减15",
            "activity2": "良品铺子73元购27元代金券",
            "bankType": "农行",
            "businessType": "零食|",
            "distance": "86m",
            "message": "良品铺子(朝阳门店)",
            "money": "人均¥68",
            "payType": "银联"
        },
        {
            "activity1": "酸奶网满30减11",
            "activity2": "酸奶网26元购7元代金券",
            "bankType": "邮政",
            "businessType": "零食|",
            "distance": "96m",
            "message": "酸奶网(朝阳门店)",
            "money": "人均¥39",
            "payType": "银联"
        },
        {
            "activity1": "万龙州海鲜满79减31",
            "activity2": "万龙州海鲜298元购65元代金券",
            "bankType": "浦发",
            "businessType": "海鲜|",
            "distance": "127m",
            "message": "万龙州海鲜(朝阳门店)",
            "money": "人均¥197",
            "payType": "银联"
        },
        {
            "activity1": "抹茶奶满25减9",
            "activity2": "抹茶奶66元购13元代金券",
            "bankType": "平安",
            "businessType": "生活|",
            "distance": "145m",
            "message": "抹茶奶(朝阳门店)",
            "money": "人均¥23",
            "payType": "银联"
        }
    ],
    "returnMessage": "请求成功",
    "returnCode": "200"
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值