1

编辑1: 我在调用和使用第二种方法的代码下方添加了我收到的错误和数据

首先很抱歉,如果我的英语不够好,如果有解决方案的另一个线程,我很抱歉。我还没有找到它。

好的,我正在制作一个使用来自网络服务的大量信息的 windows phone 8 应用程序。该应用程序制作不同的 Json Post 并使用该信息。我制作了一种发布该作品的方法:

    public static async void checkdeviceid()
    {
        //Where we are posting to:
        Uri theUri = new Uri("urlofexample");

        //Create an Http client
        HttpClient aClient = new HttpClient();
        //Class that will be serialized into Json
        objetoslistas.checkdeviceidinput paquete = new objetoslistas.checkdeviceidinput();
        //Set some values
        paquete.Text = "deviceID";
        paquete.Text2 = App.device_id;
        //serialize data
        var serializado = JsonConvert.SerializeObject(paquete);
        //Post the data
        HttpResponseMessage aResponse = await aClient.PostAsync(theUri, new StringContent(JsonConvert.SerializeObject(paquete), Encoding.UTF8, "application/json"));
        if (aResponse.IsSuccessStatusCode)
        {
            string res = await aResponse.Content.ReadAsStringAsync();
            var respuestaperfil = JsonConvert.DeserializeObject<objetoslistas.checkdeviceidoutput>(res.ToString());
            //Do something with the data
        }
        else
        {
            // show the response status code
            String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " - Reason: " + aResponse.ReasonPhrase;
        } 
    }

这段代码实际上可以工作,但事实上我的应用程序可能有 30 或 40 个不同的 POST,所以我尝试制作一个像这样的可重用方法:

public static async Task<string> jsonPOST(Uri theUri, object paquete)
    {

        //Create an Http client and set the headers we want
        HttpClient aClient = new HttpClient();

        //serialize data
        var serializado = JsonConvert.SerializeObject(paquete);


        //Post the data
        HttpResponseMessage aResponse = await aClient.PostAsync(theUri, new StringContent(JsonConvert.SerializeObject(paquete), Encoding.UTF8, "application/json"));
        if (aResponse.IsSuccessStatusCode)
        {
            string res = await aResponse.Content.ReadAsStringAsync();
            return res;
        }
        else
        {
            // show the response status code
            String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " - Reason: " + aResponse.ReasonPhrase;
            return failureMsg;
        } 
    }

这段代码主要给我带来了问题,如果它被调用的话。据我所知,问题在于,当该方法调用一些等待过程时,其余代码继续执行而不期望等待结果......

Main中的代码是:

        objetoslistas.checkdeviceidinput paquete = new objetoslistas.checkdeviceidinput();
        paquete.Text = "deviceID";
        paquete.Text2 = App.device_id;
        Uri url = new Uri("urlofhteservice");
        Task<string> recibo = metodosJson.jsonPOST(url, paquete);
        var respuestaperfil = JsonConvert.DeserializeObject<objetoslistas.checkdeviceidoutput>(recibo.ToString);
        if (respuestaperfil.Text2 == null)
        {
            IsolatedStorageSettings.ApplicationSettings["DeviceRegistered"] = true;
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings["DeviceRegistered"] = false;
        }

我收到了这个错误(从谷歌翻译翻译过来,抱歉)

There was an exception of type 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.DLL but not controlled in the user code

我在“recibo”中的数据是

        recibo  Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}" System.Threading.Tasks.Task<string>

因此,据我了解,await 方法仍在等待。

我说清楚了吗?谢谢

4

0 回答 0