1

I'm sending simple string requests with Volley to fetch JSON data. The requests are sent in onCreateView and when the user clicks a button. The first request takes a very long time (usually between 3 and 4 seconds). All other requests take between 200ms and 1.5 seconds depending on how fast the internet connection is. When I close the app and start it again, the first request again takes that long.

Any idea what causes this long delay and how I can fix it? Thanks in advance.

public class MyFragment extends Fragment {

    private static RequestQueue mQueue;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my_fragment, container, false);

        setHasOptionsMenu(true);

        if (mQueue == null) {
            mQueue = Volley.newRequestQueue(requireContext());
        }

        downloadData();

        return view;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.menu_my_fragment, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.refresh) {
                downloadData();
        }
        return super.onOptionsItemSelected(item);
    }

    private void downloadData() {

        mQueue.cancelAll("myRequests");

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://....",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("MyFragment", response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        stringRequest.setTag("myRequests");

        mQueue.add(stringRequest);
    }

.

2020-04-23 18:00:17.068 20123-20285/de.mypackage D/Volley: [9445] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://.... 0x3b23467b NORMAL 1> [lifetime=3029], [size=106467], [rc=200], [retryCount=0]
2020-04-23 18:00:17.123 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (3094 ms) [ ] https://.... 0x3b23467b NORMAL 1
2020-04-23 18:00:17.123 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+0   ) [ 2] add-to-queue
2020-04-23 18:00:17.124 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+5   ) [9444] cache-queue-take
2020-04-23 18:00:17.124 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+1   ) [9444] cache-hit-expired
2020-04-23 18:00:17.124 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+1   ) [9445] network-queue-take
2020-04-23 18:00:17.124 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+3034) [9445] network-http-complete
2020-04-23 18:00:17.125 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+8   ) [9445] network-parse-complete
2020-04-23 18:00:17.125 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+3   ) [9445] network-cache-written
2020-04-23 18:00:17.125 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+0   ) [9445] post-response
2020-04-23 18:00:17.125 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+42  ) [ 2] done
2020-04-23 18:00:20.613 20123-20284/de.mypackage D/Volley: [9444] WaitingRequestManager.maybeAddToWaitingRequests: new request, sending to network https://....
2020-04-23 18:00:20.792 20123-20175/de.mypackage I/mypackage: ProcessProfilingInfo new_methods=4359 is saved saved_to_disk=1 resolve_classes_delay=8000
2020-04-23 18:00:20.829 20123-20286/de.mypackage D/Volley: [9446] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://.... 0x3b23467b NORMAL 2> [lifetime=211], [size=106467], [rc=200], [retryCount=0]
2020-04-23 18:00:20.857 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (249  ms) [ ] https://.... 0x3b23467b NORMAL 2
2020-04-23 18:00:20.857 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+0   ) [ 2] add-to-queue
2020-04-23 18:00:20.857 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+1   ) [9444] cache-queue-take
2020-04-23 18:00:20.858 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+4   ) [9444] cache-hit-expired
2020-04-23 18:00:20.858 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+4   ) [9446] network-queue-take
2020-04-23 18:00:20.858 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+213 ) [9446] network-http-complete
2020-04-23 18:00:20.858 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+4   ) [9446] network-parse-complete
2020-04-23 18:00:20.859 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+2   ) [9446] network-cache-written
2020-04-23 18:00:20.859 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+0   ) [9446] post-response
2020-04-23 18:00:20.859 20123-20123/de.mypackage D/Volley: [2] MarkerLog.finish: (+21  ) [ 2] done

Edit: It only happens with my WiFi at home. I've also found something else interesting. The DEFAULT_TIMEOUT_MS affects the time it takes to load. When I try this, it takes 20 seconds:

stringRequest.setRetryPolicy(new DefaultRetryPolicy(20000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

Any idea?

  • Capture the network traffic at some point and look at it using Wireshark. On network level you better see what the device is doing (DNS lookup, HTTPS connection handshake, ...) and where the delay might be coming from. – Robert Apr 23 '20 at 16:15
  • I captured the traffic with Wireshark but when I use an emulator in Android Studio everything is fine. Only with a real device and WiFi, I get the big delay. Then I tested the app with two other WiFis and it worked. I guess it's just my router or internet connection that's causing the problem. –  Apr 23 '20 at 20:03

1 Answers1

0

Well it most definitely depends on the backend you send your requests to. Or some network issues. Either way - there is not much you can do about it.

The only thing you can do is not to use Volley. Use Retrofit instead - it is more mature, much easier to maintain and has close to non learning curve. Moreover it's OkHttp http client is several times faster than Volley default http client on high loads, and several percent faster on atomic requests. Volley may use OkHttp also but not out of the box(more here) - it will make them almost identical speedwise.

It is pretty much industry standard as for now.

But the biggest perk is huge community with a lot of ready answers to common problems.

Pavlo Ostasha
  • 14,527
  • 11
  • 35
  • Curious for more info on "several times faster" - what makes it faster than Volley? – Ryan M May 12 '20 at 09:23
  • Well maybe I misused Retrofit with an underlying OkHttp engine. Volley may use OkHttp either but not out of the box. I will add this to the answer – Pavlo Ostasha May 12 '20 at 11:49