I hope someone can help me, I have a queue for webservice calls and it works perfectly in Windows Phone and iOS, but not on Android.
I can't get a response from Android.
Here is my code (usage HTTPRequestQueue.AddToQueue(yourUrl) )
public class HTTPRequestQueue
{
public class QueueRequest
{
public string Url;
public Action Callback;
public string Id;
public QueueRequest(string url, Action<WebRequestCompletedArgs> callback)
{
Url = url;
Callback = callback;
}
}
enum QueueStatus { Stopped, OnGoing }; // Queue can be in one of these states
public List<QueueRequest> requests = new List<QueueRequest>();
QueueStatus currentStatus;
QueueRequest currentRequest;
HttpWebRequest WebRequest;
public HTTPRequestQueue()
{
currentStatus = QueueStatus.Stopped;
WebRequest = null;
}
public void AddToQueue(string url, Action<WebRequestCompletedArgs> callback)
{
requests.Add(new QueueRequest(url, callback));
MakeNextRequest();
}
public void Start()
{
MakeNextRequest();
}
public void StopQueue()
{
if (currentStatus == QueueStatus.OnGoing)
{
currentStatus = QueueStatus.Stopped;
if (WebRequest != null)
WebRequest.Abort();
requests.Insert(0, currentRequest);
}
}
//Private functions
void MakeNextRequest()
{
if (currentStatus == QueueStatus.OnGoing)
{
if (requests.Count() == 0)
{
currentStatus = QueueStatus.Stopped;
}
else if (requests.Count() > 0 && WebRequest == null)
{
MakeRequest();
}
}
else if (currentStatus == QueueStatus.Stopped)
{
if (requests.Count() > 0 && WebRequest == null)
{
currentStatus = QueueStatus.OnGoing;
MakeRequest();
}
}
}
void MakeRequest()
{
if (currentStatus == QueueStatus.Stopped)
return;
currentRequest = requests.First();
try
{
WebRequest = (HttpWebRequest)HttpWebRequest.Create(currentRequest.Url);
WebRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), this);
}
catch (WebException ex)
{
WebRequest = null;
if (currentRequest.Callback != null)
{
currentRequest.Callback(new WebRequestCompletedArgs(WebRequestStatus.WebException));
}
}
catch
{
if (currentRequest.Callback != null)
{
currentRequest.Callback(new WebRequestCompletedArgs(WebRequestStatus.BeginWebRequestGeneralError));
}
}
//
if (WebRequest != null)
{
if (requests.Count > 0)
requests.RemoveAt(0);
}
}
void RequestCompleted(QueueRequest request, string responseString)
{
if (request.Callback != null)
{
request.Callback(new WebRequestCompletedArgs(responseString));
}
}
void FinishWebRequest(IAsyncResult result)
{
HTTPRequestQueue ptr = (HTTPRequestQueue)result.AsyncState;
if (currentStatus == QueueStatus.Stopped)
{
ptr.WebRequest = null; //Queue is been stopped
return;
}
HttpWebRequest request = (HttpWebRequest)ptr.WebRequest;
HttpWebResponse response = null;
Stream streamResponse = null;
StreamReader streamRead = null;
WebRequestStatus status = WebRequestStatus.Completed;
bool requestSuccess = true;
try
{
// End the operation
response = (HttpWebResponse)request.EndGetResponse(result);
streamResponse = response.GetResponseStream();
streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
RequestCompleted(currentRequest, responseString); //Request completed succesfully
}
catch (WebException e)
{
requestSuccess = false;
status = WebRequestStatus.WebException;
}
catch (Exception ex)
{
requestSuccess = false;
status = WebRequestStatus.FinishWebRequestGeneralError;
}
finally
{
// Close stream objects
if (streamResponse != null)
streamResponse.Dispose();
if (streamRead != null)
streamRead.Dispose();
if (response != null)
response.Dispose();
ptr.WebRequest = null;
if (requestSuccess)
MakeNextRequest();
else
{
if (currentRequest.Callback != null)
{
currentRequest.Callback(new WebRequestCompletedArgs(status));
}
}
}
}
}