【问题标题】:Windows Phone 7 Consume Webservice JSON dataWindows Phone 7 使用 Web 服务 JSON 数据
【发布时间】:2012-06-22 18:48:46
【问题描述】:

我现在正在学习 Windows Phone 7 开发,我正在使用 C#,因为这是我最熟悉的语言。

现在,我想创建以下流程:用户在文本字段中输入内容并按下按钮 -> 我向他显示“请稍候”消息框并将文本字段文本发送到 WebService(可能结束HTTPS),当收到 WebService 响应时,我将根据响应数据向他显示另一个屏幕。

这个 WebService 只检索 JSON 数据,所以我需要解析 JSON 数据。

我想我已经能够发送数据并从服务器检索响应,但是,我不知道如何显示这个“正在加载”消息框,在 dode 时隐藏它,并启动一个新屏幕(阻止访问这是第一个)。

点击按钮:

        HttpWebRequest wr = (HttpWebRequest)System.Net.WebRequest.Create("http://example.com/");
        wr.Method = "POST";
        wr.ContentType = "application/x-www-form-urlencoded";

        wr.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), wr);

GetRequestStreamCallback 方法:

    void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest wr = (HttpWebRequest)asynchronousResult.AsyncState;
        Stream postStream = wr.EndGetRequestStream(asynchronousResult);;
        byte[] byteArray = Encoding.UTF8.GetBytes("key=" + someText.Text);
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        wr.BeginGetResponse(new AsyncCallback(GetResponseCallback), wr);
    }

GetResponseCallback 方法:

    void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest wr = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)wr.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamResponse);
            MessageBox.Show(streamReader.ReadToEnd()); // ???
            streamReader.Close();
            streamResponse.Close();
            response.Close();
        }
        catch (WebException e)
        {
            // Does nothing
        }
    }

【问题讨论】:

    标签: c# json windows-phone-7 mobile


    【解决方案1】:

    无论您对progress indication 使用什么方法,在调用服务之前将其打开,myProgressIndicator.Show = true;,然后在回调中,完成任何其他处理后,将其关闭,myProgressIndicator.Show = false;。不要忘记在 catch 中关闭它以进行异常处理。

    Coding4Fun 有一个进度叠加层。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多