保存从WCF / Web服务收到的Windows Phone中的数据。

时间:2013-01-21 04:59:15

标签: wcf web-services windows-phone-7 local-database

保存从WCF / Web服务收到的Windows Phone中的数据。 可能会在一段时间后收到响应,以便如何处理这种情况。 保存数据没有问题,但如果收到数据后如何处理

2 个答案:

答案 0 :(得分:0)

您可以使用此代码(显示我项目中的代码):

public void sendPost(string postData, Action<MyResponse, Exception> callback, CreateResponse creater)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(UrlRequest);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Accept = "application/json";
            webRequest.AllowAutoRedirect = true;
            webRequest.BeginGetRequestStream(new AsyncCallback(getRequestStreamCallback), new Request()
            {
                HttpRequest = webRequest,
                PostData = postData,
                Url = UrlRequest,
                CallBack = callback,
                Creater = creater
            });
        }

 private void getRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            var request = (Request)asynchronousResult.AsyncState;
            // End the stream request operation
            Stream postStream = request.HttpRequest.EndGetRequestStream(asynchronousResult);

            byte[] byteArray = Encoding.UTF8.GetBytes(request.PostData);

            // Add the post data to the web request
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();

            // Start the web request
            request.HttpRequest.BeginGetResponse(new AsyncCallback(getResponseCallback), request);
        }

private void getResponseCallback(IAsyncResult asynchronousResult)
        {
            var request = (Request)asynchronousResult.AsyncState;
            try
            {

                HttpWebResponse response;

                // End the get response operation
                response = (HttpWebResponse)request.HttpRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                var myResponse = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();
                MyResponse response_obj = request.Creater.CreateResponseObj();
                using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(myResponse)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(response_obj.GetType());
                    response_obj = (GYResponse)serializer.ReadObject(stream);
                    if (request.CallBack != null)
                    {
                        request.CallBack.Invoke(response_obj, null);
                    }
                }
            }
            catch (WebException e)
            {

                if (request.CallBack != null)
                {
                    request.CallBack.Invoke(null, e);
                }
            }
        }
public void getInfo(string uid, Action<MyResponse, Exception> callback)
        {
            CreateResponse creater = new CreateResponseGetInfo();
            string model = "User";
            string method = "getInfo";
            Params parametrs = new Params();
            parametrs.Uid = uid;
            //create yor request
            string request = getRequestString(model, method, parametrs, Atoken);                 
            sendPost(request, callback, creater);
        }

因此,您调用方法,该方法将请求发送到Web服务postRequester.getInfo(uid, ResponseHandler)并使用委托处理结果。

private void ResponseHandler(MyResponse result, Exception error)
        {
            if (error != null)
            {
                string err = error.Message;
                return;
            }
            else
            {
                var infoResponse = result as ResponseGetInfo;
                if (infoResponse != null)
                {
                      //result processing..              
                }

            }
        }

答案 1 :(得分:0)

您在Windows Phone应用中发出的所有网络请求都是异步的。这意味着,您从应用程序发出Web请求并附加处理程序以在响应时处理响应。在响应处理程序中,您必须处理响应并随意执行任何操作。

点击此链接Using WebClient and HttpWebRequest