我試圖用一個try/catch功能來解決這個問題,但我得到的錯誤..當我嘗試加載數據並且應用程序沒有互聯網時,我該如何避免應用程序崩潰?
無法含蓄轉換bool類型來NewtonSoft.Json.Linq.JObject。存在明確的轉換(您是否缺少演員?)
..當我嘗試返回錯誤的捕獲。這是代碼:
static public async Task<JObject> getInfo()
{
var httpClientRequest = new HttpClient();
try {
//connection
var result = await httpClientRequest.GetAsync ("localhost");
var resultString = await result.Content.ReadAsStringAsync();
var jsonResult = JObject.Parse (resultString);
return jsonResult;
} catch {
//no connection
return false; //ERROR: Cannot implicity convert type bool to NewtonSoft.Json.Linq.JObject. An explicit conversion exists (are you missing a cast?)
}
}
你想要'返回null'而不是 - 當你的函數說它返回'Task',這是編譯器錯誤的來源時,你試圖返回'false'。 –
@David T. Macknet當我返回Null,並關閉wifi並使用應用程序時,我在加載數據的頁面上出現錯誤:'未將對象引用設置爲對象的實例' – medvedo