如何在 Android 的 Retrofit 库中设置连接超时?

编程入门 行业动态 更新时间:2024-10-27 13:23:31
本文介绍了如何在 Android 的 Retrofit 库中设置连接超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的 Android 应用程序使用了 Retrofit 库.我需要将连接超时设置为 120 秒.我能怎么做 ?

I have used Retrofit library for my Android application.I need to set connection timeout as 120 Sec. How can I do ?

版本:

compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.google.code.gson:gson:2.8.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

OperatingApiClient:

OperatingApiClient:

public class OperatingApiClient { public static final String BASE_URL = "172.16.2.39/"; private static Retrofit retrofit = null; public static Retrofit getClient() { if (retrofit==null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } }

TargetFileApi 接口:

TargetFileApiInterface:

public interface TargetFileApiInterface { @GET("NNGOperating/GetTargetFileList") Call<TargetFileApiResponse> getTargetFileList(@Query("api_key") String apiKey); }

TargetFileApiResponse:

TargetFileApiResponse:

public class TargetFileApiResponse { @SerializedName("TargetFileList") private List<TargetFile> targetfileslist; public TargetFileApiResponse(List<TargetFile> targetfileslist) { this.targetfileslist = targetfileslist; } public List<TargetFile> getTargetfileslist() { return targetfileslist; } public void setTargetfileslist(List<TargetFile> targetfileslist) { this.targetfileslist = targetfileslist; } }

推荐答案

它正在工作.

public class OperatingApiClient { public static final String BASE_URL = "172.16.2.39/"; private static Retrofit retrofit = null; public static Retrofit getClient() { if (retrofit==null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } public static OkHttpClient okHttpClient = new OkHttpClient.Builder() .readTimeout(120, TimeUnit.SECONDS) .connectTimeout(120, TimeUnit.SECONDS) .build(); }

更多推荐

如何在 Android 的 Retrofit 库中设置连接超时?

本文发布于:2023-10-16 06:12:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:库中   如何在   Android   Retrofit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!