在Xamarin.Android项目的PCL项目中,当我执行httpClient.PostAsJsonAsync()
或httpClient.PutAsJsonAsync()
时,我收到以下错误。
错误:
System.TypeLoadException: Could not load type 'System.Net.Http.FormattingUtilities' from assembly 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
此代码中出现此错误:
public async Task<User> CreateUser(string name, string email, string password)
{
try
{
User user = new User();
user.Name = name;
user.Email = email;
user.Password = password;
var response = await _client.PostAsJsonAsync("myUrl", user); // error on this line
string content = await response.Content.ReadAsStringAsync();
return string.IsNullOrEmpty(content) ? new User() :
JsonConvert.DeserializeObject<User>(content);
}
catch (JsonReaderException ex)
{
throw ex;
}
catch (WebException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex; // ex is null here
}
}
答案 0 :(得分:1)
我放弃了。
我把它改为:
string json = JsonConvert.SerializeObject(user);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _client.PostAsync(myUrl, stringContent);
现在可行。