此接口处理的数据在PC终端使用时,可能会被其它应用读取,所以建议请勿使用此接口存储敏感数据。
在调用接口页面的<script>部分增加如下配置。
import fetch from '@system.fetch'
或
var fetch = require("@system.fetch") 描述
获取网络数据。
从1060版本开始,可以通过 manifest.json 的 config.network 进行网络超时的配置。
OBJECT参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| url | string | 是 | 资源url |
| data | string/object/arraybuffer | 否 | 请求的参数,可以是字符串,或者是 js 对象、arraybuffer 对象。请参见“data与Content-Type关系说明”。 说明: 设置data参数则值不能为空,如果值为空,则不传递data参数。 |
| header | object | 否 | 请求的header,会将其所有属性设置到请求的header部分。 华为私有规范(1073+):引擎将在header中强制增加Referer信息,如果开发者设置了此信息,将被覆盖。
示例:{"Accept-Encoding": "gzip, deflate","Accept-Language": "zh-CN,en-US;q=0.8,en;q=0.6","Referer":"https://quickapp.cn/com.abc.quickapp/1000/page-frame.html"} |
| method | string | 否 | 默认为 GET,可以是:OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT,PATCH(1083+) |
| responseType(1030+) | string | 否 | 支持返回类型是text,json,file,arraybuffer,默认会根据服务器返回header中的Content-Type确定返回类型,详细请参见“success返回值”。 |
| success | function | 否 | 成功返回的回调函数。 |
| fail | function | 否 | 失败的回调函数,可能会因为权限失败。 |
| complete | function | 否 | 结束的回调函数(调用成功、失败都会执行)。 |
data与Content-Type关系说明:
| data | Content-Type | 说明 |
|---|---|---|
| String | 不设置 | Content-Type 默认为 text/plain,data 值作为请求的 body。 |
| String | 任意Type | data 值作为请求的 body。 |
| Object | 不设置 | Content-Type 默认为 application/x-www-form-urlencoded,data 按照 url 规则进行 encode 拼接作为请求的 body。 |
| Object | application/x-www-form-urlencoded | data 按照 url 规则进行 encode 拼接作为请求的 body。 |
| Object | application/x-www-form-urlencoded之外的任意type | 1010 以前的版本会调用失败;从 1010 版本开始,如果 manifest 中申明的 minPlatformVersion>=1010,会将 data 转为字符串作为请求的 body。 |
| ArrayBuffer(1030+) | 不设置 | Content-Type 默认为 application/octet-stream,data 值作为请求的 body。 |
| ArrayBuffer(1030+) | 任意Type | data 值作为请求的 body。 |
success返回值:
| 参数 | 类型 | 说明 |
|---|---|---|
| code | number | 服务器状态code。 |
| data | string/object/arraybuffer/json | |
| headers | object | 服务器response的所有header。 |
responseType与success中data关系说明:
| responseType | data | 说明 |
|---|---|---|
| 无 | string | 服务器返回的 header 中 type 是 text/*或 application/json、application/javascript、application/xml,值是文本内容,否则是存储的临时文件的 uri,临时文件如果是图片或者视频内容,可以将图片设置到 image 或 video 控件上显示。 |
| text | string | 返回普通文本。 |
| json | object | 返回 js 对象。 |
| file | string | 返回存储的临时文件的 uri。 |
| arraybuffer | arraybuffer | 返回 ArrayBuffer 对象。 |
示例代码
fetch.fetch({
url:"https://www.example.com",
success:function(data){
console.log("title: " + JSON.parse(data.data).title);
},
fail: function(data, code) {
console.log("handling fail, code=" + code);
}
}) 对于HTTPS请求,对证书要求如下:
从安全性考虑,建议也支持TLS1.3。1200及以上版本在配置加密套件时排除不安全算法:TEA/DES/DESX/DES40/RC2/RC4/RIPEMD/NULL/SHA0/MD2/MD4/MD5/ANON/TLS_EMPTY_RENEGOTIATION_INFO_SCSV;1200以下版本在配置加密套件时排除不安全算法:TEA/DES/DESX/DES40/RC2/RC4/RIPEMD/eNULL/aNULL/NULL/SHA0/MD2/MD4/MD5/ANON/TLS_EMPTY_RENEGOTIATION_INFO_SCSV。部分CA可能不被操作系统信任,请开发者在选择证书时注意操作系统的相关通告。
<template>
<div>
<div>
<text>{{componentName}}</text>
</div>
<div>
<div>
<text>{{fetchData}}</text>
<image show="{{showVar}}" src="{{photoUri}}"></image>
</div>
<input type="button" onclick="refresh" value="刷新GET示例" />
</div>
<div>
<div>
<text>{{fetchData1}}</text>
</div>
<input type="button" onclick="refresh1" value="刷新arraybuffer POST示例" />
<input type="button" onclick="refresh2" value="刷新json POST示例" />
<input type="button" onclick="refresh3" value="刷新text POST示例" />
</div>
</div>
</template>
<style>
@import '../Common/css/common.css';
.image {
margin: 50px 0px;
width: 600px;
height: 140px;
}
.item-container {
margin-bottom: 50px;
margin-right: 60px;
margin-left: 60px;
flex-direction: column;
}
.item-content {
flex-direction: column;
background-color: #ffffff;
padding: 30px;
margin-bottom: 50px;
align-items: center;
justify-content: center;
}
.txt {
line-height: 45px;
padding-top: 15px;
padding-bottom: 15px;
}
</style>
<script>
import fetch from '@system.fetch'
import prompt from '@system.prompt'
export default {
data: {
componentName: 'fetch',
fetchData: '',
fetchData1: '',
photoUri: '',
showVar: true,
},
onInit: function () {
this.$page.setTitleBar({ text: 'fetch' })
// this.getphoto()
},
postarraybuffer:function(){
var that = this;
var buffer = new ArrayBuffer(4);
var a = new Uint16Array(buffer);
a[0] = 1;
a[1] = 2;
fetch.fetch({
url: "https://httpbin.org/post",
data:buffer,
responseType:'arraybuffer',
method:"POST",
success: function (ret) {
var buf1 = ret.data;
that.fetchData1 = "获取数据:" + JSON.stringify(String.fromCharCode.apply(null, new Uint8Array(buf1)));
that.showVar = true;
prompt.showToast({
message: "success"
})
},
fail: function (msg, code) {
console.log(msg, code);
},
complete: function () {
console.log("complete");
}
})
},
postjson:function(){
var that = this;
var str = '{"username":"123","password":"123"}';
fetch.fetch({
url: "https://httpbin.org/post",
data:JSON.parse(str),
header:{"User-Agent": "Mozilla/5.0 (Linux; U; Android 7.0; zh-cn; STF-AL00 Build/HUAWEISTF-AL00) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.9 Mobile Safari/537.36","Accept-Encoding": "gzip, deflate","Accept-Language": "zh-CN,en-US;q=0.8,en;q=0.6"},
responseType:'json',
method:"POST",
success: function (ret) {
that.fetchData1 = "获取数据:" + JSON.stringify(ret.data);
that.showVar = true;
prompt.showToast({
message: "success"
})
},
fail: function (msg, code) {
console.log(msg, code);
},
complete: function () {
console.log("complete");
}
})
},
poststring:function(){
var that = this;
var str = '"name"="huaweitest"&"password":"123"';
fetch.fetch({
url: "https://httpbin.org/post",
responseType:'text',
data:str,
method:"POST",
success: function (ret) {
that.fetchData1 = "获取数据:" + JSON.stringify(ret.data);
that.showVar = true;
prompt.showToast({
message: "success"
})
},
fail: function (msg, code) {
console.log(msg, code);
},
complete: function () {
console.log("complete");
}
})
},
getphoto: function () {
var that = this;
fetch.fetch({
url: "https://www.huawei.com/Assets/CBG/img/logo.png",
success: function (ret) {
that.fetchData = "获取数据:" + JSON.stringify(ret.data);// 注意that.fetchData与ret接收的数据类型保持一致
that.photoUri = ret.data;
that.showVar = true;
prompt.showToast({
message: "success"
})
},
fail: function (msg, code) {
console.log(msg, code);
},
complete: function () {
console.log("complete");
}
})
},
refresh: function () {
this.getphoto();
},
refresh1: function () {
this.postarraybuffer();
},
refresh2: function () {
this.postjson();
},
refresh3: function () {
this.poststring();
}
}
</script>