Retrofit 简单使用方法

本文介绍如何在项目中集成Retrofit与OkHttp,包括添加Maven依赖、配置Retrofit客户端、创建请求接口及调用示例。通过具体代码演示了如何实现HTTP请求的发送与响应处理。

先要添加pom依赖

        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>retrofit</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>converter-jackson</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.retrofit2</groupId>
            <artifactId>converter-scalars</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>3.14.9</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.14.9</version>
        </dependency>

添加RetrofitConfig

@Configuration
@Slf4j
public class RetrofitConfig {

    @Value("${retrofit.backendUrl:}")
    private String backendUrl  ;

    @Bean
    public BackendClient backendClient() {
        return this.build(backendUrl, BackendClient.class);
    }

    ;

    private OkHttpClient retrofitOkHttpClient;

    private <T> T build(String baseUrl, Class<T> type) {
        OkHttpClient client = getRetrofitOkHttpClient();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create((new ObjectMapper())
                        .registerModule((Module) new ParameterNamesModule())
                        .registerModule((Module) new Jdk8Module())
                        .registerModule((Module) new JavaTimeModule())))
                .client(client)
                .build();
        return retrofit.create(type);
    }

    private synchronized OkHttpClient getRetrofitOkHttpClient() {
        if (retrofitOkHttpClient == null) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor(log::info);
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            retrofitOkHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(logging)
                    .connectTimeout(180, TimeUnit.SECONDS)
                    .readTimeout(180, TimeUnit.SECONDS)
                    .build();
        }
        return retrofitOkHttpClient;
    }
}

编写请求类

public interface BackendClient {
    
    @GET("/queryForThird")
    Call<JSONObject> queryForThird();

}

 调用时直接注入BackendClient即可直接使用,如下:

backendClient.queryForThird().execute().body();
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值