我想要调用服务器,获取JSON结果并将结果返回给调用方法。下面是我调用服务器的代码:
public async Task<Dictionary<string,string>> callAjax(string mthd,Dictionary<string,string> values)
{
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://dev.adex-intl.com/adex/mvc/receiving/"+mthd, content);
var responseString = await response.Content.ReadAsStringAsync();
Dictionary<string,string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>((string)responseString);
return result;
}
这是调用上述方法的代码:
public void loginPressed(object sender, EventArgs e)
{
if(String.IsNullOrEmpty(badge.Text)) {
DisplayAlert("Error", "Enter your badge number", "Ok");
} else {
IsBusy = true;
var parameters = new Dictionary<string, string>
{
{ "badgeNumber", badge.Text }
};
GlobalMethods globalMethods = new GlobalMethods();
Dictionary<string,string> results = globalMethods.callAjax("login", parameters);
var id = results["userid"].ToString();
}
}
我收到编译错误“无法隐式转换类型'System.Threading.Tasks.Task'”行“Dictionary results = globalMethods.callAjax(”login“,parameters);”
答案 0 :(得分:0)
callAjax
是一种异步方法,因此在调用它时需要使用await
Dictionary<string,string> results = await globalMethods.callAjax("login", parameters);