Xamarin Forms HttpClient PostAsync不返回响应

编程入门 行业动态 更新时间:2024-10-26 14:34:53
本文介绍了Xamarin Forms HttpClient PostAsync不返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试通过传递服务url和json参数来获取API响应.网址和参数已正确传递给 requestAPI 函数,但没有提供 PostAsync 方法的响应.网址是API位置,参数是类别ID.当我在浏览器中运行相同的API时,它会给出正确的响应.但不在应用程序中.

I'm trying to get API response by passing service url and json parameter. Url and Parameter passing properly to the requestAPI function, but doesn't give response from PostAsync method. Url is the API location, parameter is the Category Id. When I'm running same API in the browser, it gives correct response. But not in app.

这是 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 }; } }

类别ID来自此方法.

private async void loadBookList(string categoryId) { IsBusy = true; if (CrossConnectivity.Current.IsConnected) { ResponseObject responseObject = await _apiService.requestAPI("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.

  • 用作uri的网址var uri = new Uri(urlString);
  • jsonParameter也用作字符串
  • 还使用了
  • GetAsync
  • Url used as a uri var uri = new Uri(urlString);
  • jsonParameter also used as a string
  • GetAsync also used
  • 在此方面的任何帮助将不胜感激.

    Any assistance on this will be greatly appreciated.

    推荐答案

    如果您正在Android上运行它,并且代码在PCL中,请尝试添加ModernHttpClient,以便可以使用本机HttpClientHandler.

    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.

    github/paulcbetts/ModernHttpClient

    但是我也建议所有人从PCL升级到.NET标准.如果这样做,则不必安装NuGet包即可使用HttpCliennt.然后,您可以在项目设置中选择HttpClientHandler的实现.

    But I also recommend all to upgrade from PCL to .NET standard. If you do so you don't have to install a NuGet package to use HttpCliennt. And then you can select implementation of HttpClientHandler in project settings.

    更多推荐

    Xamarin Forms HttpClient PostAsync不返回响应

    本文发布于:2023-11-08 22:09:13,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1570586.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Forms   Xamarin   PostAsync   HttpClient

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!