在Retrofit Android中如何设置超时时间?

4

实际上,我通过Retrofit将一个txt文件发送到服务器,一切都很顺利,即使没有连接,我也能捕获onFailure方法。

但是问题是当我离开Wi-Fi区域时,我发送文件,然后重新进入Wi-Fi区域,文件将被发送,但是onFailure方法被激活了。

那么我该如何为onResponse方法设置超时或类似的内容呢?

这是我的Activity中的sendPost方法:

 @SuppressLint("DefaultLocale")
    public void sendPost() {
        @SuppressLint({"SdCardPath", "DefaultLocale"}) final File file = new File("/data/data/com.example.igardini.visualposmobile/files/"+String.format("%03d", Integer.valueOf(nTerminalino))+"_"+nTavoli.getText().toString()+".txt");

        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);

        RequestBody fbody = RequestBody.create(MediaType.parse("text/*"), file); builder.addFormDataPart(String.format("%03d", Integer.valueOf(nTerminalino))+"_"+nTavoli.getText().toString(), file.getName(),fbody);

        MultipartBody requestBody = builder.build();

        APIService apiService = ApiUtils.getAPIService(ipCASSA);

        Call<Void> call = apiService.savePost(requestBody);

        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
                if (response.isSuccessful()) {
                    Log.i("RESPONSE: ", response.toString());
                    Print();
                    file.delete();
                    dialogLoading.dismiss();
                    Runtime.getRuntime().gc();
                } else {
                        //
                }
            }

            @Override
            public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {
                Log.e("TAG", t.toString());
                MediaPlayer mpFound = MediaPlayer.create(pterm.this,R.raw.errorsound);
                mpFound.start();
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                if (v.hasVibrator()) {
                    v.vibrate(6000);
                } else {
                    //
                }
                new GlideToast.makeToast(pterm.this, "CONNESSIONE FALLITA!", GlideToast.LENGTHLONG, GlideToast.FAILTOAST).show();
                dialogLoading.dismiss();
            }
        });
    }

以下是 ApiUtils 类的内容:

class ApiUtils {

    private ApiUtils() {}



    static APIService getAPIService(String ipCASSA) {

        String BASE_URL = "http://"+ipCASSA+"/WebQuery/";

        return RetrofitClient.getClient(BASE_URL).create(APIService.class);
    }
}

这里是 RetrofitClient 类:

public class RetrofitClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

这里有一个APIService

public interface APIService {

    @POST("UPD.aspx?CART=PTERM")
    Call<Void> savePost(@Body RequestBody text);
}

2
请查看此链接 https://dev59.com/fVoU5IYBdhLWcg3wvIqp,它会对你有所帮助。 - Nilesh Panchal
@NileshPanchal,好的,谢谢。 - NiceToMytyuk
1个回答

10
请添加如下内容:
OkHttpClient okHttpClient = new OkHttpClient.Builder()  
        .connectTimeout(1, TimeUnit.MINUTES)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(15, TimeUnit.SECONDS)
        .build();

Retrofit.Builder builder = new Retrofit.Builder()  
        .baseUrl("http://10.0.2.2:3000/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create());

还可以查看这个简易教程

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接