如题,我这个就直接上代码吧
(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);