我有两个Android应用程序使用Volley调用与Web服务器通信:一个应用程序等待另一个发布短消息。大部分时间这都可以正常工作:等待的应用程序从发布应用程序获取响应。但是,每隔5次左右,响应将由第一个应用程序发送,但第二个应用程序永远不会得到响应。以下是相关代码:
发布代码:
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);
}
我知道服务器正在处理来自服务器日志的请求:
10212211883390475 2017-11-26, 17:57:40 42
10212211883390475 2017-11-26, 17:57:40 43 4 s
我花了好几天时间而不是一个排球错误,我看不出问题是什么。有什么想法吗?
答案 0 :(得分:2)
问题是由于Apache服务器在五分钟后超时。当我将Apache中的超时设置更改为-1(无限超时)时,问题得以解决。