实际上,我正在通过改造发送一个TXT文件到我的服务器,一切正常,我甚至赶上了失败的方法,如果好像没有连接。
但当我离开wi-fi区域,然后发送文件,并在wi-fi区域中重新输入要发送的文件,但onfailure方法被激活时,问题就来了。
那么,我怎样才能像onresponse方法的超时那样发送呢?
以下是活动中的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);
    }
}

这里是改装客户类:
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);
}

最佳答案

添加如下

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

09-16 14:29