C# 转换任务<;字符串>;串

C# 转换任务<;字符串>;串,c#,async-await,win-universal-app,C#,Async Await,Win Universal App,我想在通用windows phone应用程序中解析JSON文件,但无法将任务转换为字符串 public MainPage() { this.InitializeComponent(); HttpClient httpClient = new HttpClient(); String responseLine; JObject o; try { string respo

我想在通用windows phone应用程序中解析JSON文件,但无法将任务转换为字符串

public MainPage()
    {
        this.InitializeComponent();

        HttpClient httpClient = new HttpClient();
        String responseLine;
        JObject o;
        try
        {
            string responseBodyAsText;

            HttpResponseMessage response = httpClient.GetAsync("http://localhost/list.php").Result;

            //response = await client.PostAsync(url, new FormUrlEncodedContent(values));
            response.EnsureSuccessStatusCode();
            responseBodyAsText = response.Content.ReadAsStringAsync().Result;
           // responseLine = responseBodyAsText;
              string Website = "http://localhost/list.php";
            Task<string> datatask =  httpClient.GetStringAsync(new Uri(string.Format(Website, DateTime.UtcNow.Ticks)));
            string data = await datatask;
            o = JObject.Parse(data);
            Debug.WriteLine("firstname:" + o["id"][0]);
        }
        catch (HttpRequestException hre)
        {
        }

如何修复它?

在构造函数中不能使用
wait
。您需要为此创建一个
async
方法

一般来说,我不建议使用
async void
,但是当您从构造函数调用它时,这是有道理的

public MainPage()
{
    this.InitializeComponent();
    this.LoadContents();
}

private async void LoadContents()
{
    HttpClient httpClient = new HttpClient();
    String responseLine;
    JObject o;
    try
    {
        string responseBodyAsText;

        HttpResponseMessage response = await httpClient.GetAsync("http://localhost/list.php");

        //response = await client.PostAsync(url, new FormUrlEncodedContent(values));
        response.EnsureSuccessStatusCode();
        responseBodyAsText = await response.Content.ReadAsStringAsync();
       // responseLine = responseBodyAsText;
          string Website = "http://localhost/list.php";
        Task<string> datatask =  httpClient.GetStringAsync(new Uri(string.Format(Website, DateTime.UtcNow.Ticks)));
        string data = await datatask;
        o = JObject.Parse(data);
        Debug.WriteLine("firstname:" + o["id"][0]);
    }
    catch (HttpRequestException hre)
    {
        // You might want to actually handle the exception
        // instead of silently swallowing it.
    }
}
public主页()
{
this.InitializeComponent();
这是LoadContents();
}
私有异步void LoadContents()
{
HttpClient HttpClient=新HttpClient();
弦响应线;
JObject o;
尝试
{
字符串responseBodyAsText;
HttpResponseMessage response=等待httpClient.GetAsync(“http://localhost/list.php");
//response=wait client.PostAsync(url,新FormUrlEncodedContent(值));
response.EnsureSuccessStatusCode();
responseBodyAsText=wait response.Content.ReadAsStringAsync();
//responseLine=responseBodyAsText;
字符串网站=”http://localhost/list.php";
Task datatask=httpClient.GetStringAsync(新Uri(string.Format(Website,DateTime.UtcNow.Ticks));
字符串数据=等待数据任务;
o=JObject.Parse(数据);
Debug.WriteLine(“名字:+o[“id”][0]);
}
捕获(HttpRequestException hre)
{
//您可能希望实际处理该异常
//而不是默默地吞下它。
}
}
查看,您可以使用:Result属性获得该属性

例如:

    Task<int> task1 = myAsyncMethod(); //You can also use var instead of Task<int>
    int i = task1.Result; 
taskTask1=myAsyncMethod()//您还可以使用var代替Task
int i=任务1.结果;
试试这个:

Task post=postPostAsync(“Url”,data).Result.Content.ReadAsStringAsync();
post.Result.ToString();

我尝试了这段代码,但出现了以下错误:Newtonsoft.Json.JsonReaderException类型的异常出现在Newtonsoft.Json.dll中,但未在user.dll中处理code@AlaEddineHelmiHaouala,检查调试器下的
数据
字符串。很可能您正在处理格式错误的JSON(或非JSON内容)。感谢您的回答,但当我调用async方法来显示我的产品列表时,它不会显示在模拟器中,而是显示在控制台中。她有什么问题?好的做法是将用户任务作为返回类型而不是Void。我们不应该从异步方法中使用void工作?在类构造函数中放置一个阻塞方法,例如
Task.Result
,这不是一个好主意。@Peregrine为什么不呢?对我有用;)欢迎来到SO!请阅读旅游和一个问题。这个问题是4年前提出的。@goodjujuju回答4岁或以上的问题是个问题吗?我这样问是因为我自己回答了很多8-9岁的问题,我从来没有得到过这样做是不合适的反馈。
    Task<int> task1 = myAsyncMethod(); //You can also use var instead of Task<int>
    int i = task1.Result;