1. 程式人生 > >Android網路請求開源框架retrofit的基本GET用法(2.4版本)

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"
}