7

I want to parse from a JSON file in a universal windows phone application but I can't convert task to string

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

I have error in this line

 string data = await datatask;

how can I fix it?

Kirill Shlenskiy
  • 9,367
  • 27
  • 39

3 Answers3

8

Looking into this documentation, you can get that using: Result property.

For Example:

    Task<int> task1 = myAsyncMethod(); //You can also use var instead of Task<int>
    int i = task1.Result; 
Amer
  • 319
  • 3
  • 7
4

You cannot use await inside a constructor. You'll need to create an async method for that.

Generally I don't recommend using async void, but when you're calling it from the constructor it's somewhat justified.

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.
    }
}
Kirill Shlenskiy
  • 9,367
  • 27
  • 39
  • I try this code but I get this error An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code – Ala Eddine Helmi Haouala Feb 18 '16 at 00:02
  • @AlaEddineHelmiHaouala, inspect your `data` string under the debugger. Chances are you're dealing with malformed JSON (or non-JSON content). – Kirill Shlenskiy Feb 18 '16 at 00:12
  • thank's for your answer but when I called the async method to show a list of the my product it is not shown in the emulator but it is shown in the console. what's the problem her ? – Ala Eddine Helmi Haouala Feb 21 '16 at 11:51
  • 1
    The good practice is to user Task as return type instead of Void. We should not use void from the Async method. – Jayee Feb 15 '19 at 02:58
1

Try this:

Task<string> post = postPostAsync("Url", data).Result.Content.ReadAsStringAsync();
post.Result.ToString();
Yosef Bernal
  • 1,006
  • 9
  • 20
sahar
  • 21
  • 1
  • Welcome to SO! Please read the tour [tour] and [answer] a question. This question was asked over 4 years ago. – GoodJuJu Dec 16 '20 at 14:15
  • 1
    @GoodJuJu is it a problem to answer questions that are 4 years old or older? I am asking because I have answered quite a lot questions that are 8-9 years old myself, and I never got feedback that it's an inappropriate thing to do. – Theodor Zoulias Dec 16 '20 at 15:36