美文网首页Android知识Android开发Android技术知识
使用Retrofit2进行HTTP请求设置请求超时

使用Retrofit2进行HTTP请求设置请求超时

作者: 淡日临窗 | 来源:发表于2017-05-22 18:14 被阅读713次

采用Retrofit2本身可以进行优雅的RESTFul请求,但是无法设置请求超时时间,需要配合okhttp3来设置请求超时.

使用步骤

  1. 添加依赖:
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
  1. 添加配置信息并设置超时时间
OkHttpClient.Builder httpBuilder=new OkHttpClient.Builder();
OkHttpClient client=httpBuilder.readTimeout(3, TimeUnit.MINUTES)
                .connectTimeout(3, TimeUnit.MINUTES).writeTimeout(3, TimeUnit.MINUTES) //设置超时
                .build();
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URLString.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

相关文章

网友评论

    本文标题: 使用Retrofit2进行HTTP请求设置请求超时

    本文链接:https://www.haomeiwen.com/subject/wqrixxtx.html