The problem is that with this approach in the jsonPost(qrData) method there are several requests to the server, and only one is needed.

  @Override public void receiveDetections(Detector.Detections<Barcode> detections) { final SparseArray<Barcode> qrcodes = detections.getDetectedItems(); if (qrcodes.size() != 0) { txtResult.post(new Runnable() { @Override public void run() { txtResult.setText(qrcodes.valueAt(0).displayValue); qrData = qrcodes.valueAt(0).displayValue; String text = jsonPost(qrData); Log.d("Text", text); if (text.contains("success")) { Toast.makeText(getApplicationContext(), "Операція успішна", Toast.LENGTH_LONG).show(); cameraSource.release(); cameraSource.stop(); Intent intent = new Intent(MinusBonusesActivity.this, MainActivity.class); startActivity(intent); } else { Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show(); } } }); } } }); } public String jsonPost(String qrData) { String url = "http://admin.bonsitecrm.com/api/customer/add_bonus_manager"; StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // response Log.d("Response", response); responseFromServer = response; } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // error Log.d("Error.Response", error.toString()); } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("json", "{ \"user_id\":\"216\", \"customer_id\":\"6664\", \"operation\":\"minus\", \"amount\":\"100\", \"subject\":\"Тест\", \"access_token\":\"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF\" }"); Log.d("Map", params.toString()); return params; } }; requestQueue.add(postRequest); Log.d("ResponseFromServer", responseFromServer); return responseFromServer; } 

Console output:

  01-11 16:18:37.877 26887-26887/com.work.golinko.shkafbonuses D/ActivityThreadInjector: clearCachedDrawables. 01-11 16:18:38.128 26887-26887/com.work.golinko.shkafbonuses D/Camera: app passed NULL surface 01-11 16:18:38.334 26887-27087/com.work.golinko.shkafbonuses D/OpenGLRenderer: endAllActiveAnimators on 0x7fa0ba2800 (RippleDrawable) with handle 0x7f8cf40e20 01-11 16:18:38.850 26887-26887/com.work.golinko.shkafbonuses I/Timeline: Timeline: Activity_launch_request time:623583262 01-11 16:18:38.929 26887-26887/com.work.golinko.shkafbonuses W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite not found. 01-11 16:18:38.935 26887-26887/com.work.golinko.shkafbonuses I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite:0 and remote module com.google.android.gms.vision.dynamite:1402 01-11 16:18:38.935 26887-26887/com.work.golinko.shkafbonuses I/DynamiteModule: Selected remote version of com.google.android.gms.vision.dynamite, version >= 1402 01-11 16:18:38.955 26887-26887/com.work.golinko.shkafbonuses I/Vision: Loading library libbarhopper.so 01-11 16:18:38.956 26887-26887/com.work.golinko.shkafbonuses I/Vision: libbarhopper.so library load status: true 01-11 16:18:38.978 26887-26887/com.work.golinko.shkafbonuses D/ActivityThreadInjector: clearCachedDrawables. 01-11 16:18:39.977 26887-26887/com.work.golinko.shkafbonuses I/Choreographer: Skipped 59 frames! The application may be doing too much work on its main thread. 01-11 16:18:42.531 26887-3531/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.720 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.722 26887-3534/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.722 26887-3531/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.722 26887-3532/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.731 26887-3533/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.738 26887-26887/com.work.golinko.shkafbonuses D/ResponseFromServer: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.738 26887-26887/com.work.golinko.shkafbonuses D/Text: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.789 26887-26887/com.work.golinko.shkafbonuses D/Camera: app passed NULL surface 01-11 16:18:42.816 26887-3531/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.865 26887-3533/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.868 26887-3532/com.work.golinko.shkafbonuses D/Map: {json={ "user_id":"216", "customer_id":"6664", "operation":"minus", "amount":"100", "subject":"Тест", "access_token":"VHvjY3TFsSrDlbCg2MaeHInvyRGqQchMUN5SQZyjRZWdbytelEs3mPyUHelyrCklpqegpMAhMwuX5YbrCEvYu3TbheKIR43huiyU58bRFGPLE1dgGIfaL9m2o6LgbPxF" }} 01-11 16:18:42.949 26887-26887/com.work.golinko.shkafbonuses I/Timeline: Timeline: Activity_launch_request time:623587362 01-11 16:18:42.986 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.987 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.987 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.987 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.987 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.998 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:42.998 26887-26887/com.work.golinko.shkafbonuses D/Response: {"status":"success","code":"0","message":"success"} 01-11 16:18:43.041 26887-26887/com.work.golinko.shkafbonuses D/ActivityThreadInjector: clearCachedDrawables. 
  • At least logs would be filtered. - MrBin
  • I have doubts that the method is called several times, since the scanner reads constantly and, with any successful reading, it calls your method. More information and code is needed. - MrBin

1 answer 1

It was possible to solve this problem by adding a flag to disable the qr reader while sending a request to the server

 public void receiveDetections(Detector.Detections<Barcode> detections) { final SparseArray<Barcode> qrcodes = detections.getDetectedItems(); if (qrcodes.size() != 0) { //this "isRun" will help us to stop qr reader during post request if (!isRun) { txtResult.post(QRTask = new Runnable() { @Override public void run() { isRun = true; qrData = qrcodes.valueAt(0).displayValue; spinner.setVisibility(View.VISIBLE); jsonPost(qrData); } }); } } 

And also by changing the default settings in the Volley library, since it waited for 2.5 seconds for a response from the server and, if it didn’t wait, sent the request again.

  // here we change RetryPolicy because by default Volley will wait for 2.5 seconds and then sends new request postRequest.setRetryPolicy(new DefaultRetryPolicy(5000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); requestQueue.add(postRequest); 

Here is a link to the entire project - https://github.com/Chumva/QR_Bonuses/blob/master/app/src/main/java/com/work/golinko/shkafbonuses/AddBonusesActivity.java