在Android改装中设置超时时间?

时间:2018-09-28 13:18:19

标签: android http timeout retrofit

实际上,我正在通过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);
}

1 个答案:

答案 0 :(得分:1)

只需添加如下内容

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());

您也可以选中此easy tutorial

相关问题