有时,Volley无法从服务器返回响应 [英] Occasionally, Volley fails to return a response from the server
问题描述
我有两个Android应用程序,它们通过Volley调用与Web服务器通信:一个应用程序等待另一应用程序发布短消息.在大多数情况下,此方法运行良好:正在等待的应用程序从发布的应用程序中获取响应.但是,每5次左右,第一个应用就会发送响应,而第二个应用再也不会响应.这是相关代码:
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:
邮政编码:
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);
}
等待代码:
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
除了Volley错误外,我已经花了几天时间,但我看不出问题出在哪里.有什么想法吗?
I've spent several days on this and other than a Volley bug, I can't see what the problem is. Any thoughts?
推荐答案
问题是由于五分钟后Apache服务器超时而导致的.当我将Apache中的超时设置更改为-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).
这篇关于有时,Volley无法从服务器返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!