简体   繁体   中英

Xamarin.Forms - HttpClient GetAsync or PostAsync sometimes takes too long

I am using Xamarin.Forms and I am using HttpClient GetAsync and PostAsync to make calls to an api, my problem is the client is complaining that the application is too slow when it makes an api call. Is there anyways I can speed up this process or is there another faster way to call an api? Here is an example method:

public async Task<List<SubCatClass>> GetSubCategories(int category)
        {
            var client = new HttpClient();

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("category", category.ToString())
            });

            var response = await client.PostAsync(string.Format("https://exmample.com/api/index.php?action=getSubCategories"), content);

            var responseString = await response.Content.ReadAsStringAsync();

            List<SubCatClass> items = JsonConvert.DeserializeObject<List<SubCatClass>>(responseString);

            return items;

        }

And here is how I am calling it.

await webService.GetSubCategories(item.categoryid);

The api I have full control over the code (PHP) and the server.

Any help would be much appreciated.

Thanks in advance.

UPDATE

I called the api in postman and here was the results

在此处输入图像描述

You can try using ModernHttpClient , it will increase your speed than using default by Xamarin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM