我最近从一个旧的apache库切换到了volley,但是我不能让它以类似的方式工作。
我正试图用截击向端点发送请求,但我意识到当手机没有良好的信号/互联网连接时可能会出现问题。我不希望服务器收到垃圾邮件与排队的请求,但我也希望一个请求被发送尽快互联网连接允许它。
如何使用volley处理较长时间内的连接丢失,而不使服务器崩溃,并保持在连接足够好时将请求发送到服务器的可能性?
我曾尝试使用网络链接调节器来模拟一个坏连接(65%的数据包丢失),同时计时请求通过所需的时间,apache请求似乎通过得更快,而不会向服务器发送垃圾邮件。
下面是我目前使用的代码:
while(requestNotConfirmedOnServer()) {
numberOfTries++;
if(numberOfTries <= Constants.ADD_ALARM_RETRIES) {
final CaStringRequest stringRequest = new CaStringRequest(_context, Request.Method.GET, false, addURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
wsRequestIsInProgress = false;
//Handle response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
wsRequestIsInProgress = false;
Log.d(TAG, "VolleyError: " + error);
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(60000, 0, 1f));
if(requestNotConfirmedOnServer()) {
CARequestQueue.getInstance(_context).addToRequestQueue(stringRequest);
wsRequestIsInProgress = true;
}
try {
while(wsRequestIsInProgress) {
Thread.sleep(1000);
}
Thread.sleep(Constants.ADD_ALARM_RETRY_TIMEOUT);
} catch (InterruptedException e) {
ApplicationManager.getInstance(_context).printLog(TAG, "Error while putting thread to sleep: ");
}
}
}
另外,旧的apache代码:
while(requestNotConfirmedOnServer()){
try{
String addURL = "http..."
ApplicationManager.getInstance(_context).printLog(TAG, addURL);
CCResponse response= new WebServicesManager().sendHttpRequest(_context, addURL);
if(response.get_httpCode()==HttpStatus.SC_OK){
String[] responseParts = response._responseString.split(";");
//Handle Response
}
}catch(Exception e){
ApplicationManager.getInstance(_context).printLog(TAG, "Error: ");
}
ApplicationManager.getInstance(_context).printLog(TAG, "Putting thread to sleep.");
try {
Thread.sleep(Constants.ADD_ALARM_SLEEP_INTERVAL);
} catch (InterruptedException e) {
ApplicationManager.getInstance(_context).printLog(TAG, "Error while putting thread to sleep: ");
}
}
//From WebServiceManager
public CCResponse sendHttpPostOrRequest(Context context, String relativeURL, String postData) {
CCResponse ccResponse;
try {
String finalURL = "http://....";
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpResponse response = null;
if(postData != null){
HttpPost httpPost = new HttpPost(finalURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("data", postData));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httpPost);
}else {
response = httpclient.execute(new HttpGet(finalURL));
}
HttpEntity resEntityGet = response.getEntity();
String responseString = EntityUtils.toString(resEntityGet, "UTF-8");
ccResponse = new CCResponse(response.getStatusLine().getStatusCode(), responseString);
Log.d(TAG, responseString);
}catch (Exception e) {
Log.d(TAG, "Exception: " + e);
ccResponse = new CCResponse(-1, "Exception " + e);
}
return ccResponse;
}
暂无答案!
目前还没有任何答案,快来回答吧!