如何使用Windows Phone 8在Periodic Task中发布数据?

时间:2014-04-05 07:58:46

标签: c# .net windows-phone-7 windows-phone-8

我试过这段代码,但我不明白为什么它不起作用。但是,当我逐步调试时,它的工作原理。为什么?我想这是时间问题?当它自动运行时,更新功能有问题吗?

谢谢,我是Windows手机8的新手,如果有人能提供一些我会很感激的例子!!

public static void Update(int mid, string mname, double mgLon, double mgLat)
    {
        Lon = mgLon; Lat = mgLat; id = mid; name = mname;

        try
        {
            HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri("*****", UriKind.RelativeOrAbsolute));
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }   
    }

    private static void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
        try
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            // End the stream request operation
            Stream postStream = myRequest.EndGetRequestStream(callbackResult);

            // Create the post data
            string postData = "key=*****&tableid=******&data=" + "{\"_id\":\"" + id + "\",\"_location\":\"" + Lon + "," + Lat + "\",\"_name\":\"" + name + "\"}";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

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

            // Start the web request
            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);

        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }

    private static void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
            using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
            {
                string result = httpWebStreamReader.ReadToEnd();
                //For debug: show results
                Debug.WriteLine(result);
                response.Close();
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }

0 个答案:

没有答案