我在我的应用程序中使用改造库,我想设置一个60秒的超时。改造有办法吗?
我把改造设成这样:
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer(BuildConfig.BASE_URL)
.setConverter(new GsonConverter(gson))
.build();我如何设置超时?
发布于 2015-04-01 00:26:12
您可以在基础HTTP客户端上设置超时。如果不指定客户端,Retrofit将创建一个具有默认连接和读取超时的客户机。要设置自己的超时,需要配置自己的客户机并将其提供给RestAdapter.Builder。
一个选项是使用OkHttp客户机,也来自Square。
1.添加库依赖项
在build.gradle中,包括以下一行:
compile 'com.squareup.okhttp:okhttp:x.x.x'其中x.x.x是所需的库版本。
2.设置客户端
例如,如果您希望设置60秒的超时,请在版本2之前对Retrofit执行此操作,在版本3之前执行Okhttp (更新版本的,请参见编辑):
public RestAdapter providesRestAdapter(Gson gson) {
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
return new RestAdapter.Builder()
.setEndpoint(BuildConfig.BASE_URL)
.setConverter(new GsonConverter(gson))
.setClient(new OkClient(okHttpClient))
.build();
}编辑1
对于自3.x.x以来的okhttp版本,您必须以这种方式设置依赖项:
compile 'com.squareup.okhttp3:okhttp:x.x.x'并使用构建器模式设置客户端:
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();关于超时的更多信息
编辑2
由于2.x.x也使用构建器模式,所以修改了版本,所以将上面的返回块更改为:
return new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();如果使用类似于我的providesRestAdapter方法的代码,那么将方法返回类型更改为改造。
关于改造2-从1.9升级指南的更多信息
ps:如果你的minSdkVersion大于8,你可以使用TimeUnit.MINUTES
okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);有关单位的更多详细信息,请参见TimeUnit。
发布于 2016-06-27 09:46:58
这些答案对我来说已经过时了,所以这是如何解决的。
添加OkHttp,在我的例子中,版本是3.3.1
compile 'com.squareup.okhttp3:okhttp:3.3.1'然后,在构建您的改造之前,请执行以下操作:
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();发布于 2018-12-03 05:43:14
public class ApiClient {
private static Retrofit retrofit = null;
private static final Object LOCK = new Object();
public static void clear() {
synchronized (LOCK) {
retrofit = null;
}
}
public static Retrofit getClient() {
synchronized (LOCK) {
if (retrofit == null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(40, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(Constants.WEB_SERVICE)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
public static RequestBody plain(String content) {
return getRequestBody("text/plain", content);
}
public static RequestBody getRequestBody(String type, String content) {
return RequestBody.create(MediaType.parse(type), content);
}
}
@FormUrlEncoded
@POST("architect/project_list_Design_files")
Call<DesignListModel> getProjectDesign(
@Field("project_id") String project_id);
@Multipart
@POST("architect/upload_design")
Call<BoqListModel> getUpLoadDesign(
@Part("user_id") RequestBody user_id,
@Part("request_id") RequestBody request_id,
@Part List<MultipartBody.Part> image_file,
@Part List<MultipartBody.Part> design_upload_doc);
private void getMyProjectList()
{
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<MyProjectListModel> call = apiService.getMyProjectList("",Sorting,latitude,longitude,Search,Offset+"",Limit);
call.enqueue(new Callback<MyProjectListModel>() {
@Override
public void onResponse(Call<MyProjectListModel> call, Response<MyProjectListModel> response) {
try {
Log.e("response",response.body()+"");
} catch (Exception e)
{
Log.e("onResponse: ", e.toString());
}
}
@Override
public void onFailure(Call<MyProjectListModel> call, Throwable t)
{
Log.e( "onFailure: ",t.toString());
}
});
}
// file upload
private void getUpload(String path,String id)
{
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
MultipartBody.Part GalleryImage = null;
if (path!="")
{
File file = new File(path);
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
GalleryImage = MultipartBody.Part.createFormData("image", file.getName(), reqFile);
}
RequestBody UserId = RequestBody.create(MediaType.parse("text/plain"), id);
Call<uplod_file> call = apiService.geUplodFileCall(UserId,GalleryImage);
call.enqueue(new Callback<uplod_file>() {
@Override
public void onResponse(Call<uplod_file> call, Response<uplod_file> response) {
try {
Log.e("response",response.body()+"");
Toast.makeText(getApplicationContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();
} catch (Exception e)
{
Log.e("onResponse: ", e.toString());
}
}
@Override
public void onFailure(Call<uplod_file> call, Throwable t)
{
Log.e( "onFailure: ",t.toString());
}
});
}
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'https://stackoverflow.com/questions/29380844
复制相似问题