Occasionally, Volley fails to return a response from the server

六月ゝ 毕业季﹏ 提交于 2019-12-11 06:18:42

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/47499490/occasionally-volley-fails-to-return-a-response-from-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!