I try to make an awaitable request in my PCL/Android/iOs project. Function for request is in PCL
public class DataService
{
private static string _requestUri = "https://requesturi.com/";
public static async Task<LocalizationData> GetLocalization(string code)
{
string queryString = _requestUri + "get_localization.php" + "?code=" + code;
HttpClient client = new HttpClient();
var response = await client.GetAsync(queryString);
dynamic data = null;
if (response != null)
{
string json = response.Content.ReadAsStringAsync().Result;
data = JsonConvert.DeserializeObject(json);
if (data["status"] == "success")
{
List<string> aliases = new List<string>();
List<string> translations = new List<string>();
foreach (var localization in data["localizations"])
{
aliases.Add((string)localization["alias"]);
translations.Add((string)localization["translation"]);
}
LocalizationData localizationData = new LocalizationData(code, aliases.ToArray(), translations.ToArray());
return localizationData;
}
else
{
return null;
}
}
return null;
}
}
Both in Android and iOS I call this function with
localizationData = await DataService.GetLocalization(langCode);
In Android it works without problems both on simulator and on real device. But when I try run it in iOS, on simulator it works fine, on real device app crash on
var response = await client.GetAsync(queryString);
Is it something about permissions? Or something else? Can anybody help me with this problem?
UPDATED
There are exception for client.GetAsync(queryString) I get in app on real device: "Operation is not valid due to the current state of the object"
According to the thread in Xamarin forum this is issue with Reference. Seems like httpClient instance was created in mono memory but not in iOS memory, due to a difference between an iOS device(AOT) and simulator(JIT) build nature.
Try :
1) Go to References of ios Project
2) Edit References
3) Check 'System.Net.Http'
In general - use ModernHttpClient - it provides wrappers for native networking API, it is secure and faster then default .Net HttpClient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.