如何使用OKHttp库取消请求?

时间:2019-06-02 21:12:16

标签: java android okhttp3

我正在使用use std::collections::{HashMap, HashSet}; use std::hash::Hash; pub trait Point2: Eq + Hash {} pub trait Link2<'a, A, B>: Eq + Hash { fn origin(&self, x: i8) -> Option<&'a A>; fn set_origin(&mut self, x: i8, o: A); fn target(&self, x: i8) -> Option<&'a B>; fn set_target(&mut self, x: i8, t: B); } pub struct Web<'a, V, E> where V: Point2, E: Link2<'a, V, V>, { map: HashSet<(V, E, V)>, inc: HashMap<&'a V, HashSet<&'a E>>, } impl<'a, V, E> Web<'a, V, E> where V: Point2, E: Link2<'a, V, V>, { pub fn new(map: HashSet<(V, E, V)>) -> Self { let mut y = Self { map, inc: HashMap::new(), }; for x in &(y).map { if let Some(val) = y.inc.get_mut(&x.2) { val.insert(&x.1); } else { let mut z = HashSet::new(); &z.insert(&x.1); y.inc.insert(&x.2, z); } } y } } 库来请求文件信息,如下所示:

error[E0515]: cannot return value referencing local data `y.map`
  --> src/lib.rs:43:9
   |
33 |         for x in &(y).map {
   |                  -------- `y.map` is borrowed here
...
43 |         y
   |         ^ returns a value referencing data owned by the current function

error[E0505]: cannot move out of `y` because it is borrowed
  --> src/lib.rs:43:9
   |
22 | impl<'a, V, E> Web<'a, V, E>
   |      -- lifetime `'a` defined here
...
33 |         for x in &(y).map {
   |                  -------- borrow of `y.map` occurs here
...
43 |         y
   |         ^
   |         |
   |         move out of `y` occurs here
   |         returning this value requires that `y.map` is borrowed for `'a`

要取消请求,我使用此代码,如this所说:

OKHttp

但是 final String url = "my_url"; final String tag = "tag"; final OkHttpClient client = new OkHttpClient(); Thread t = new Thread(new Runnable() { @Override public void run() { try { Request request = new Request.Builder() .url(url) .get() .tag(tag) .build(); Call call = client.newCall(request); Response response = call.execute(); //work with response object, etc. } catch (IOException e) { System.out.println("Error: " + e.toString()); } } }); t.start(); 列表和 for (Call call : mHttpClient.dispatcher().queuedCalls()) { if (call.request().tag().equals("tag")) call.cancel(); } for (Call call : mHttpClient.dispatcher().runningCalls()) { if (call.request().tag().equals("tag")) call.cancel(); } 列表的大小为零。为什么?如何在OKHttp中取消请求?谢谢。

0 个答案:

没有答案