Forum Xamarin.Forms

Nothing happens when using HTTPClient GetStringAsync(JSONurl)

I'm having trouble loading JSON data to my listview. No error comes up, but when debugging, I've found that nothing happens after GetStringAsync(JSONurl) is executed. App continues to run, but my activity indicator spins infinitely. Here's my code:

`private const string Url = "http://MY_URL.json";
private HttpClient client = new HttpClient();

protected async void OnGetList()
{
if (CrossConnectivity.Current.IsConnected)
{

            try
            {
                //Activity indicator visibility on
                activity_indicator.IsRunning = true;
                //Getting JSON data from the Web
                var content = await client.GetStringAsync(Url);
                //We deserialize the JSON data from this line
                var tr = JsonConvert.DeserializeObject<List<AfhData>>(content);
                //After deserializing , we store our data in the List called ObservableCollection
                ObservableCollection<AfhData> trends = new ObservableCollection<AfhData>(tr);

                //Then finally we attach the List to the ListView. Seems Simple :)
                myList.ItemsSource = trends;


                //We check the number of Items in the Observable Collection
                int i = trends.Count;
                if (i > 0)
                {
                    //If they are greater than Zero we stop the activity indicator
                    activity_indicator.IsRunning = false;
                }

                //Here we Wrap  the size of the ListView according to the number of Items which have been retrieved 
                i = (trends.Count * heightRowsList);
                activity_indicator.HeightRequest = i;

            }
            catch (Exception ey)
            {
                Debug.WriteLine("" + ey);
            }

        }`
Tagged:

Answers

  • JohnHardmanJohnHardman GBUniversity mod
    edited November 2019

    @SteveRussell said:
    I've found that nothing happens after GetStringAsync(JSONurl) is executed. App continues to run, but my activity indicator spins infinitely

    The use of "await" when calling GetStringAsync means that the remaining code in OnGetList() will not execute until GetStringAsync completes. How long have you waited to see if GetStringAsync completes? Particularly with a previously unused HttpClient instance, it can take a while. It's also dependent on the server's response time, communications issues etc.

    I suggest using a browser on your test device to open the Url (shown in the code as "http://MY_URL.json"). Does that show the data that you expect? If so, execute your code just afterwards to see if it then works.

    Also, your Url is using http rather than https. If that is what you want, have you made the appropriate tweaks to allow http to be used in your app?

  • SteveRussellSteveRussell Member ✭✭✭
    Thanks for your reply.

    I’ve left my S9 (physical device) running for 5+ minutes and nothing.

    Sorry, it was https I just changed it to http to test and forgot to change it back.

    I will try to open the URL on my device in its browser see if anything comes up. It does on my PC though

    I think the issue is with my Visual Studio. You can see from my previous questions posted recently that I’m having unusual activity with my VS. I tried running from another PC and the activity indicator seemed to disappear after 1s, but nothing was displayed. That’s possibly another issue that I will investigate tomorrow.
  • SteveRussellSteveRussell Member ✭✭✭
    Tested the link on the device browser, it works fine. Now the activity indicator disappears, and after debugging it seems like all the data is collected and should essentially display on the device, but nothing is shown. I don’t see how it can be a XAML issue, but I’m not sure what else it could be
  • SteveRussellSteveRussell Member ✭✭✭

    Works now, it was a problem with setting height of the listview

Sign In or Register to comment.