Igo*_*yuk 6 android http retrofit retrofit2
实际上我在我的应用程序中使用Retrofit2将一些文本发送到我的服务器.
问题是当我使用射频设备时,网络中断,所以我正在失败,呼叫应该被取消,但一旦设备重新连接到网络似乎它重新发送文本或类似的文本保持在队列中,我会处理它.
这是我的方法,我发送文本(我已经尝试在onFailure中使用call.cancel(),但没有效果)
@SuppressLint("DefaultLocale")
public void sendPost() {
@SuppressLint({"SdCardPath", "DefaultLocale"}) final File file = new File("/data/data/app/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();
final APIService apiService = ApiUtils.getAPIService(ipCASSA);
final Call<Void> calls = apiService.savePost(requestBody);
calls.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 {
calls.cancel();
}
}
@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);
}
new GlideToast.makeToast(pterm.this, "CONNESSIONE FALLITA!", GlideToast.LENGTHLONG, GlideToast.FAILTOAST).show();
dialogLoading.dismiss();
}
});
}
Run Code Online (Sandbox Code Playgroud)
在我正在构建OkHttpClient的类中,我设置了retryOnConnectionFailure(false),但仍然无法解决即使onFailure我收到POST请求的问题.
更新:
经过一些研究后,我发现OkHttp在连接失败时对POST进行了无声重试.
那么我怎样才能阻止客户端重试?
一些信息:
我的应用程序是一个服务员的应用程序,所以当我收到一条onFailure消息,我通知服务员,POS没有收到收据,但尚未打印,但OkHttpClient仍然重试发送文件,我强迫服务员重新发送收据后,onFailure它会导致打印重复的收据.
这是我的ApiUtils类,APIService接口和RetrofitClient类
class ApiUtils {
private ApiUtils() {}
static APIService getAPIService(String ipCASSA) {
String BASE_URL = "http://"+ipCASSA+"/web/";
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10000, TimeUnit.MILLISECONDS)
.writeTimeout(10000, TimeUnit.MILLISECONDS)
.readTimeout(10000, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(false)
.build();
return RetrofitClient.getClient(BASE_URL,okHttpClient).create(APIService.class);
}
}
public interface APIService {
@POST("CART=PTERM")
Call<Void> savePost(@Body RequestBody text);
}
class RetrofitClient {
private static Retrofit retrofit = null;
static Retrofit getClient(String baseUrl, OkHttpClient okHttpClient) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Run Code Online (Sandbox Code Playgroud)
由于retryOnConnectionFailure(false)没有帮助,问题不是由 OKHttp 重试引起的。
我怀疑您正在重复该请求。检查是否sendPost()被多次调用。