我正在创建这样一个线程内的改造问题:
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();
我需要通过 progressDialog.setButton()
从 Thread
外部取消 onResponse
中的调用
。这怎么可能?
感谢任何帮助!
最佳答案
您需要获取 mService.uploadFile(body)
的实例。这是retrofit2.Call<T>
与公共(public)方法的接口(interface) cancel()
。
...{
call = mService.uploadFile(body)
call.enqueue(new Callback<String>() {
...
像这样使用它
void cancel() {
if (!call.isCanceled && call.isExecuted) call.cancel()
}
还有方法 enqueue(Callback<T> callback)
Asynchronously send the request and notify callback of its response or if an error occurred talking to the server, creating the request, or processing the response.
您不需要使用Thread()
来处理它。
关于java - 如何从相关线程外部取消改造调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59253205/