如何使用OKHTTP取消请求

问题描述

关于以下内容

            //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();
            }

解决方法

我需要能够 使用 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并取消请求,但仍然不断!

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”