コード例 #1
0
        private void ResposeCallbackBase(IAsyncResult result, GoogleDirectionNotFoundDelegate notfoundDelegate,
                                         GoogleDirectionFoundDelegate foundDelegate, GoogleDirectionFailedDelegate failedDelegate)
        {
            var state         = (List <object>)result.AsyncState;
            var googleRequest = (GoogleDirectionRequest)state[1];

            if (result.IsCompleted)
            {
                try
                {
                    var request              = (HttpWebRequest)state[0];
                    var response             = (HttpWebResponse)request.EndGetResponse(result);
                    var responseStream       = response.GetResponseStream();
                    var serializer           = new DataContractJsonSerializer(typeof(GoogleDirectionResponse));
                    var googleSearchResponse = (GoogleDirectionResponse)serializer.ReadObject(responseStream);
                    if (googleSearchResponse.Status == GoogleDirectionResponseStatusCode.OK)
                    {
                        foundDelegate(googleSearchResponse, googleRequest);
                    }
                    else if (googleSearchResponse.Status == GoogleDirectionResponseStatusCode.ZERO_RESULTS)
                    {
                        notfoundDelegate(googleSearchResponse, googleRequest);
                    }
                }
                catch (Exception e)
                {
                    if (failedDelegate != null)
                    {
                        failedDelegate(e.Message, e);
                    }
                }
            }
            else
            {
                if (failedDelegate == null)
                {
                    throw new Exception("For some reason request is not completed");
                }
                failedDelegate("For some reason request is not completed", null);
            }
        }
コード例 #2
0
        private void ResposeCallbackBase(IAsyncResult result, GoogleDirectionNotFoundDelegate notfoundDelegate,
            GoogleDirectionFoundDelegate foundDelegate, GoogleDirectionFailedDelegate failedDelegate)
        {
            var state = (List<object>)result.AsyncState;
            var googleRequest = (GoogleDirectionRequest)state[1];
            if (result.IsCompleted)
            {
                try
                {
                    var request = (HttpWebRequest)state[0];
                    var response = (HttpWebResponse)request.EndGetResponse(result);
                    var responseStream = response.GetResponseStream();
                    var serializer = new DataContractJsonSerializer(typeof(GoogleDirectionResponse));
                    var googleSearchResponse = (GoogleDirectionResponse)serializer.ReadObject(responseStream);
                    if (googleSearchResponse.Status == GoogleDirectionResponseStatusCode.OK)
                    {
                        foundDelegate(googleSearchResponse, googleRequest);
                    }
                    else if (googleSearchResponse.Status == GoogleDirectionResponseStatusCode.ZERO_RESULTS)
                    {
                        notfoundDelegate(googleSearchResponse, googleRequest);
                    }
                }
                catch (Exception e)
                {
                    if (failedDelegate != null)
                     failedDelegate(e.Message, e);
                }
            }
            else
            {
                if (failedDelegate == null) throw new Exception("For some reason request is not completed");
                failedDelegate("For some reason request is not completed", null);

            }
        }