我有一个使用ProgressDialog的应用。但问题是,每当我调用我的web服务时,加载图标就会冻结(停止旋转),直到我收到来自我的Web服务的响应。我怎么能克服这个?
以下是我的代码:
ProgressDialog pdLoading;
pdLoading = new ProgressDialog(this);
pdLoading.SetMessage("Submiting your query. Please wait...");
pdLoading.SetCanceledOnTouchOutside(false);
pdLoading.Show();
//send to server
HttpClient client = new HttpClient();
var jsonParameter = JsonConvert.SerializeObject(obj);
var content = new StringContent(jsonParameter.ToString(), Encoding.UTF8, "application/json");
//The progress dialog freezes when it calls this method
HttpResponseMessage result = client.PostAsync(WsClasses.GetWsLink() + "/XXX/value1", content).Result;
我也尝试了以下解决方案
public async System.Threading.Tasks.Task<HttpResponseMessage> PostAsync(object inputData, string wsLink)
{
HttpClient client = new HttpClient();
var jsonParameter = JsonConvert.SerializeObject(inputData);
var content = new StringContent(jsonParameter.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage result = await client.PostAsync(wsLink, content);
if (client != null)
client.Dispose();
if (pdLoading != null)
pdLoading.Dismiss();
return result;
}
答案 0 :(得分:0)
使用await not .result。 Await将允许应用程序在调用Web服务时运行。 .result将导致应用程序在调用Web服务时冻结
HttpResponseMessage result = await client.PostAsync(WsClasses.GetWsLink() + "/XXX/value1", content)