首页 > 解决方案 > 改造 2 使请求编译但抛出 sockettimeoutexception

问题描述

我在这里遇到了最奇怪的问题。我正在使用改造来发出请求,但起初我收到套接字超时异常,我认为这是后端问题,但它正在处理邮递员。然后当我们检查后端日志时,我们意识到请求将被编译并提供 200 个代码,但我没有得到任何响应!!!!!!

这是我的代码

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

这是我的代码初始化改造

private static Retrofit cRetrofit = null;
private static Retrofit retrofit = null;
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

public static final int CACHE_EX_TIME = 60;
public static Retrofit getClient(String baseUrl) {
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();
    httpClient.connectTimeout(15, TimeUnit.SECONDS);
    httpClient.readTimeout(15, TimeUnit.SECONDS);
    httpClient.writeTimeout(15, TimeUnit.SECONDS);
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()

                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(httpClient.build())
                .build();
    }
    return retrofit;
}

标签: androidretrofitokhttp

解决方案


推荐阅读