【问题标题】:HttpClient PostAsync does not respond in case of windows phone but works fine in android and iosHttpClient PostAsync 在 windows phone 的情况下不响应,但在 android 和 ios 中工作正常
【发布时间】:2015-12-04 22:53:59
【问题描述】:
HttpResponseMessage myResponse = myClient.PostAsync(theUri, theContent).Result;

我在theUri 中传递uritheContent 中的json 数据

这在 android 和 ios 中运行良好,但在 windows phone 中没有响应,这句话有什么问题

【问题讨论】:

  • 您可以尝试发送包裹在StringContent 对象中的数据吗?
  • 在 windows phone 中它甚至不向服务器发送请求
  • 嗯,你需要识别why
  • 我不确定,但设置是否有任何更改,以便向服务器发送未从 Windows Phone 商店下载的应用程序的请求?

标签: xamarin.forms


【解决方案1】:

如果你使用 Result 意味着你的方法会阻塞。但不建议这样做,因为它会导致死锁。使用 Result 属性或 Wait() 时需要小心。

当您使用异步方法时,我建议您声明异步方法并使用 await 调用该函数。

public async Task Method()
{
    HttpResponseMessage myResponse = await myClient.PostAsync(theUri, theContent);
}

如果您不能将方法声明为异步并且您需要使用异步方法。我建议您使用此类在同步方法中转换异步方法: How would I run an async Task<T> method synchronously?

【讨论】:

  • 现在我正在使用 async await 现在它确实陷入僵局,但问题是我无法在 myResponse 中获得结果我认为仍然没有从应用程序发送请求到服务(我不确定)跨度>
  • 我建议你使用链接的帮助类并在catch中放置一个断点,因为有时会隐藏错误。
  • 某处我读到使用 await TaskScheduler.Default.SwitchTo(); // 当从 UI 线程阻塞 Result 时。但我在“等待 TaskScheduler.Default.SwitchTo();”处收到错误错误:“System.Threading.Tasks.TaskScheduler”不包含“SwitchTo”的定义,并且找不到接受“System.Threading.Tasks.TaskScheduler”类型的第一个参数的扩展方法“SwitchTo”(您是否缺少使用指令还是程序集引用?)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多