如題,我這個就直接上代碼吧
(function () { let request = http.request; // 覆蓋http關鍵函數request,其他http返回最終會調用這個函數 http.request = function () { try { // 捕捉所有異常 return request.apply(http, arguments); } catch (e) { // 出現異常返回null console.error(e); return null; } } })(); var url = "https://baidu.com"; //設置超時為10秒 http.__okhttp__.setTimeout(10000); // 獲取一個不存在的網站,應該會Timeout (或者把網絡斷開) console.log(http.get(url)); //控制台打印返回內容 toast("程序結束"); //解析json數據 var text = '{ "sites" : [' + '{ "name":"Runoob" , "url":"www.runoob.com" },' + '{ "name":"Google" , "url":"www.google.com" },' + '{ "name":"Taobao" , "url":"www.taobao.com" } ]}'; objj = JSON.parse(text); //輸出json化的原始內容,不這樣做,在處理autojshttpget抓出來的網頁源代碼會出錯 console.log(objj); //計算site里有幾個子內容的代碼 var count = Object.keys(objj.sites).length; var i=2; toastLog(count); toastLog(objj.sites[i].name + " " + objj.sites[i].url);
