我需要能够 使用 OKHTTP* 来 管理 一些请求,使用来通过输入地址来接收一些预测。该 问题是 ,我插入每次 CHAR
它将使一个新的请求,但在同一时间,我需要取消前一个!例如:纽约市=同时请求13个!因此,我仅使用一个实例来尝试取消已请求但没有成功的任何内容。这就是我所做的!
Google Places AutoComplete
*Call
Address.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(Address.getText().toString().length() > 3){
_Address = Address.getText().toString();
if(call != null){
call.cancel();
}
Request request = new Request.Builder()
.url(getPlaceAutoCompleteUrl(_Address))
.addHeader("content-type", "application/json")
.addHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; google_sdk Build/MR1) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
.build();
call = client.newCall(request);
call.enqueue(new Callback() {
public void onResponse(Call call, final Response response) throws IOException {
final String result = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("Response",result);
PlacePredictions place = null;
try {
place = LoganSquare.parse(result,PlacePredictions.class);
} catch (IOException e) {
e.printStackTrace();
}
if(autoCompleteAdapter == null){
autoCompleteAdapter = new AutoCompleteAdapter(CustomPlaces.this);
recyclerView.setAdapter(autoCompleteAdapter);
autoCompleteAdapter.Add(place.getPlaces());
}else {
autoCompleteAdapter.Clear();
autoCompleteAdapter.Add(place.getPlaces());
autoCompleteAdapter.notifyDataSetChanged();
}
}
});
}
public void onFailure(Call call, IOException e) {
//fail();
}
});
}else {
if(autoCompleteAdapter != null){
autoCompleteAdapter.Clear();
}
}
}
我检查call
对象是否为null并取消请求,但仍然不断!
关于以下内容:
//Set tags for your requests when you build them:
Request request = new Request.Builder().
url(url).tag("requestKey").build();
//When you want to cancel:
//A) go through the queued calls and cancel if the tag matches:
for (Call call : mHttpClient.dispatcher().queuedCalls()) {
if (call.request().tag().equals("requestKey"))
call.cancel();
}
//B) go through the running calls and cancel if the tag matches:
for (Call call : mHttpClient.dispatcher().runningCalls()) {
if (call.request().tag().equals("requestKey"))
call.cancel();
}
我阅读了一些将jsons发布到服务器的示例。 有人说: OkHttp是Java提供的HttpUrlConnection接口的实现。它提供了一个用于写入内容的输入流,而不知道(或关心)该内容是什么格式。 现在我想用name和password参数对URL做一个普通的post。 这意味着我需要自己将名称和值对编码成流?
问题内容: 我读了一些将jsons发布到服务器的示例。 有人说: OkHttp是Java提供的HttpUrlConnection接口的实现。它提供用于编写内容的输入流,并且不知道(或不在乎)内容的格式。 现在,我想用名称和密码的参数对URL进行常规发布。 这意味着我需要自己将名称和值对编码为流? 问题答案: 当前接受的答案已过期。现在,如果您想创建一个发布请求并向其中添加参数,则应该使用Mul
目前,我正在捕捉异常并重新尝试维护一个计数器变量。
我需要使50 http获取请求尽快与Android的改型。我正在使用OKHTTP的改型。目前,与普通Java和相比,Retrofit的工作做得很差:对于所有50个请求,如果我为设置池大小20,则为Retrofit设置50sec,为普通设置30sec,而为Retrofit/OkHttp设置。 如果查看logcat,我可以看到无论在中设置了什么,registfit最多执行5个并发请求,而在中,并发请求
OKHTTP在某种程度上支持并发请求API吗? 如果没有,最好的实现方法是什么?
问题内容: 有一个新的API用于从JavaScript发出请求:fetch()。是否有任何内置的机制可以在飞行中取消这些请求? 问题答案: 现在支持截至2017年9月20日的参数,但目前 并非所有浏览器都支持此参数 。 2020更新: 大多数主流浏览器(Edge,Firefox,Chrome,Safari,Opera和其他一些浏览器)都支持该功能,该功能已成为DOM生活标准的一部分。(截至2020