I'm creating such an on-thread issue of retrofit:
new Thread(new Runnable() {
@Override
public void run() {
mService.uploadFile(body)
.enqueue(new Callback<String>() {
@Override
public void onResponse(final Call<String> call, Response<String> response) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "uploaded!", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}).start();
I need to cancel the call in onResponse from outside the Thread via a progressDialog.setButton(). How is this possible?
Any help is Appreciated!
You need to get instance of
mService.uploadFile(body). This isretrofit2.Call<T>interface with public methodcancel().And use it like this
Also metohd
enqueue(Callback<T> callback)You don't need to use
Thread()to deal with it.