. 0 min read
This tip is small but very useful if you are working with OKHTTP3 library. With the help of this code, you can cancel current or queue request of OkHTTP 3.
OkHttpClient client = new OkHttpClient(); MediaType MEDIA_TYPE = MediaType.parse("application/json"); JSONObject postData = new JSONObject(); try { postData.put("q", "My Query"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } RequestBody body = RequestBody.create(MEDIA_TYPE, postData.toString()); Request request = new Request.Builder() .url(Urls.search) .post(body) .header("Accept", "application/json") .header("Content-Type", "application/json") .tag("preRequest") .build(); // Cancel queue request for (Call call : client.dispatcher().queuedCalls()) { if (call.request().tag().equals("requestKey")) call.cancel(); } // Cancel last request for (Call call : client.dispatcher().runningCalls()) { if (call.request().tag().equals("preRequest")) { Log.e("cancle", "requestCancel"); call.cancel(); } } client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { String mMessage = e.getMessage().toString(); Log.w("failure Response", mMessage); call.cancel(); } @Override public void onResponse(Call call, Response response) throws IOException { final String mMessage = response.body().string(); Search.this.runOnUiThread(new Runnable() { @Override public void run() { try { /** Your response **/ } catch (JSONException e) { e.printStackTrace(); } } }); } });
Hope this small piece of code help you.
Implement Google Mobile Ads in your React Native application to allows you to monetize your app with AdMob.
Learn how to deploy React and Node.Js application using CI/CD pipeline with Github Actions.
DevOps is a set of practices, tools, and a cultural philosophy that automate and integrate the processes between software development and IT teams. It emphasizes team empowerment, cross-team communication and collaboration, and technology automation.
Learn different between find(), filter() and map() in javascript with example and other array methods in javascript.
MongoDB is an open-source document-oriented database that is designed to store a large scale of data and also allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval o