简体   繁体   English

Xamarin Forms HttpClient PostAsync不返回响应

[英]Xamarin Forms HttpClient PostAsync does not return response

I'm trying to get API response by passing service url and json parameter. 我正在尝试通过传递服务url和json参数来获取API响应。 Url and Parameter passing properly to the requestAPI function, but doesn't give response from PostAsync method. 网址和参数正确传递给requestAPI函数,但未提供PostAsync方法的响应。 Url is the API location, parameter is the Category Id. 网址是API位置,参数是类别ID。 When I'm running same API in the browser, it gives correct response. 当我在浏览器中运行相同的API时,它会给出正确的响应。 But not in app. 但不在应用程序中。

This is requestAPI function. 这是requestAPI函数。

public async Task<ResponseObject> requestAPI(string urlString, string jsonParameters)
        {
            await Task.Delay(2000); // NOTE: just to simulate a HTTP request over the wire

            try
            {
                var json = JsonConvert.SerializeObject(jsonParameters);
                HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                HttpResponseMessage response = null;

                if (true)       // no issue here.
                {                                                    
                    response = await client.PostAsync(urlString, httpContent);
                }
                else
                {
                    response = await client.PutAsync(urlString, httpContent);
                }

                if (response.IsSuccessStatusCode)
                {
                    Debug.WriteLine(@"              TodoItem successfully saved.");
                    var returnVal = await response.Content.ReadAsStringAsync();
                    return new ResponseObject{
                        jsonString = returnVal,
                        isSuccess = true,
                        error = null
                    };
                }
                else
                {
                    return new ResponseObject
                    {
                        jsonString = null,
                        isSuccess = false,
                        error = null
                    };
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"              ERROR {0}", ex.Message);
                return new ResponseObject
                {
                    jsonString = null,

                    isSuccess = false,
                    error = null
                };
            }
        }

Category Id comes from this method. 类别ID来自此方法。

private async void loadBookList(string categoryId)
        {
            IsBusy = true;

            if (CrossConnectivity.Current.IsConnected)
            {
                ResponseObject responseObject = await _apiService.requestAPI("http://192.168.0.35/kiyawamu-api/index.php/rest/V1/mobileappintegration/getbookdetailsbycategory", Convert.ToString(categoryId));

                if (responseObject.isSuccess)
                {
                    var jsonObject = JsonConvert.DeserializeObject<BookListJsonObject>(responseObject.jsonString);
                    CategoryBooks = new ObservableCollection<Book>(jsonObject.booksByCategory);
                }
                else
                {
                    giveAlertForCommonError();
                }
            }
        }

I tried following solutions, but doesn't work. 我尝试了以下解决方案,但不起作用。

  1. Url used as a uri var uri = new Uri(urlString); 用作uri的网址var uri = new Uri(urlString);
  2. jsonParameter also used as a string jsonParameter也用作字符串
  3. GetAsync also used 还使用了GetAsync

Any assistance on this will be greatly appreciated. 在这方面的任何帮助将不胜感激。

If you're running it on Android and the code is in a PCL, try to add ModernHttpClient so you can use the native HttpClientHandler. 如果您在Android上运行它,并且代码在PCL中,请尝试添加ModernHttpClient,以便可以使用本机HttpClientHandler。

https://github.com/paulcbetts/ModernHttpClient https://github.com/paulcbetts/ModernHttpClient

But I also recommend all to upgrade from PCL to .NET standard. 但我也建议所有人从PCL升级到.NET标准。 If you do so you don't have to install a NuGet package to use HttpCliennt. 如果这样做,则不必安装NuGet包即可使用HttpCliennt。 And then you can select implementation of HttpClientHandler in project settings. 然后,您可以在项目设置中选择HttpClientHandler的实现。

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

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