【问题标题】:Saving data in windows phone received from WCF/web service .保存从 WCF/web 服务接收到的 windows phone 中的数据。
【发布时间】:2013-01-21 04:59:14
【问题描述】:

保存从 WCF/web 服务接收到的 windows phone 中的数据。 可能会在一段时间后收到响应,因此如何处理这种情况。 保存数据没问题,但是数据接收晚了怎么处理

【问题讨论】:

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


    【解决方案1】:

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

    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);
            }
    

    因此,您调用方法,该方法将请求发送到网络服务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..              
                    }
    
                }
            }
    

    【讨论】:

      【解决方案2】:

      您在 Windows Phone 应用程序中发出的所有 Web 请求都是异步的。这意味着,您从您的应用程序发出一个 Web 请求并附加一个处理程序来处理响应时的响应。在响应处理程序中,您必须处理响应并使用它做任何您想做的事情。

      查看此链接Using WebClient and HttpWebRequest

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多