简体   繁体   English

Xamarin.Forms - HttpClient GetAsync 或 PostAsync 有时需要太长时间

[英]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. 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?无论如何我可以加快这个过程,还是有另一种更快的方法来调用 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. api 我可以完全控制代码 (PHP) 和服务器。

Any help would be much appreciated.任何帮助将非常感激。

Thanks in advance.提前致谢。

UPDATE更新

I called the api in postman and here was the results我在 postman 中调用了 api,这是结果

在此处输入图像描述

You can try using ModernHttpClient , it will increase your speed than using default by Xamarin您可以尝试使用ModernHttpClient ,它会比默认使用 Xamarin 提高您的速度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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