1

I have two Android apps that communicate with a Web server using Volley calls: one app waits for the other to post a short message. Most of the time this works fine: the waiting app gets the response from the posting app. However, every 5 or so times, the response is sent by the first app but the second one never gets a response. Here is the relevant code:

Posting code:

    private synchronized void setGameProgress(String user_id, int pos, String letter, String accessToken) {
    String url = "";

    RequestQueue queue = Volley.newRequestQueue(activity);

    try {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                spinner.setVisibility(View.VISIBLE);
            }
        });
        url = "https://www.chiaramail.com:443/GameServer/GameServer?user_ID=" + URLEncoder.encode(user_id, "UTF-8") + "&token=" + URLEncoder.encode(accessToken, "UTF-8") + "&cmd=" + URLEncoder.encode("SETGAME PROGRESS ", "UTF-8") + "&parms=" + URLEncoder.encode(user_id + " " + pos + " " + letter, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Toast.makeText(activity, getString(R.string.error_updating_progress) + e.getMessage(), Toast.LENGTH_LONG).show();
        spinner.setVisibility(View.INVISIBLE);
    }

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (!response.startsWith("42 ")) {
                        queue_builder.setMessage(getString(R.string.error_updating_progress) + " " + response);
                        queue_alert = queue_builder.create();
                        queue_alert.show();
                    }
                    spinner.setVisibility(View.INVISIBLE);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            volleyError = error;
            Toast.makeText(activity, getString(R.string.error_updating_progress) + volleyError.getMessage(), Toast.LENGTH_LONG).show();
            spinner.setVisibility(View.INVISIBLE);
        }
    });
    queue.add(stringRequest);
}

Wait code:

private synchronized void getGameProgress(final String user_id, final String accessToken) {
    String url = "";

    RequestQueue queue = Volley.newRequestQueue(activity);

    try {
        url = "https://www.chiaramail.com:443/GameServer/GameServer?user_ID=" + URLEncoder.encode(user_id, "UTF-8") + "&token=" + URLEncoder.encode(accessToken, "UTF-8") + "&cmd=" + URLEncoder.encode("GETGAME PROGRESS ", "UTF-8") + "&parms=" + URLEncoder.encode(user_id, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Toast.makeText(activity, getString(R.string.error_getting_progress) + e.getMessage(), Toast.LENGTH_LONG).show();
    }

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.startsWith("43 ")) {
                        StringTokenizer st = new StringTokenizer(response.substring(3));
                        String position = st.nextToken();
                        String letter = st.nextToken();
                        updateTheirProgress(Integer.valueOf(position), letter);
                        getGameProgress(opponent_ID, AccessToken.getCurrentAccessToken().getToken());
                    } else {
                        queue_builder.setMessage(getString(R.string.error_getting_progress) + " " + response);
                        queue_alert = queue_builder.create();
                        queue_alert.show();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            volleyError = error;
            Toast.makeText(activity, getString(R.string.error_getting_progress) + volleyError.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            60000, 5,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    queue.add(stringRequest);
}

I know that the server is processing the requests from the server log:

10212211883390475 2017-11-26, 17:57:40 42 
10212211883390475 2017-11-26, 17:57:40 43 4 s

I've spent several days on this and other than a Volley bug, I can't see what the problem is. Any thoughts?

FractalBob
  • 3,225
  • 4
  • 29
  • 40
  • 1
    What I understood is that you have two separate apps, waiting app and posting app, one sends a request to the server and then the server should send the response to the waiting app; correct? If so I don't see how volley has to do with the bug, isn't it that the server is not sending the response for the waiting app? And are you sending it as a notification to the waiting app? – riadrifai Nov 26 '17 at 19:34
  • Actually, they're the same app, running on different mobile devices (the app is a turn-based game). The server log, above, proves that the posting app is sending a response: the line reading "42" is a response code sent immediately to the posting app. After the server receives the message, it issues a notifyAll() to wake up all the threads that are waiting for the post, so "43 4 s" is the response. – FractalBob Nov 26 '17 at 21:09

1 Answers1

2

The problem was due to the Apache server timing out after five minutes. The problem was resolved when I changed the timeout setting in Apache to -1 (infinite timeout).

FractalBob
  • 3,225
  • 4
  • 29
  • 40