使用requestInStream接口需要注意,callback回调返回的是number类型的数据,也就是响应码,不会返回具体的数据,可通过on("dataReceive")接收响应数据,当订阅成功时,error为undefined,data为接收到的http流式数据,类型为ArrayBuffer;否则为错误对象。
import { http } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
class Header {
public contentType: string;
constructor(contentType: string) {
this.contentType = contentType;
}
}
function createAndRequest() {
let httpRequest = http.createHttp();
let options: http.HttpRequestOptions = {
method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET。
// 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定。
extraData: 'data to send',
expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型。
usingCache: true, // 可选,默认为true。
priority: 1, // 可选,默认为1。
// 开发者根据自身业务需要添加header字段。
header: new Header('application/json'),
readTimeout: 60000, // 可选,默认为60000ms。
connectTimeout: 60000, // 可选,默认为60000ms。
usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定。
usingProxy: false, // 可选,默认不使用网络代理,自API 10开始支持该属性。
};
httpRequest.requestInStream('EXAMPLE_URL', options, (err: BusinessError<void>, data: number) => {
if (!err) {
console.info('requestInStream OK! ResponseCode is ' + JSON.stringify(data));
} else {
console.error('requestInStream ERROR : err = ' + JSON.stringify(err));
}
});
httpRequest.on('dataReceive', (data: ArrayBuffer) => {
console.info('dataReceive length: ' + JSON.stringify(data.byteLength));
});
httpRequest.on('dataEnd', () => {
console.info('Receive dataEnd !');
httpRequest.destroy();
});
}
@Entry
@Component
struct Index {
build() {
RelativeContainer() {
Button('click')
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
createAndRequest();
});
}
.height('100%')
.width('100%');
}
} 当所有数据接收完毕后,on('dataEnd', () => {})方法会被调用,标志着数据接收完成。在使用完http请求对象后,调用destroy()方法来主动销毁这个对象,避免资源泄露。
确保on('headersReceive')在requestInStream之前同步执行,且无异步间隙:
let httpRequest = http.createHttp();
httpRequest.on('headersReceive', (header: Object) => {
console.info(`header: ${JSON.stringify(header)}`);
});
// 之后再调用requestInStream
httpRequest.requestInStream("EXAMPLE_URL", options).then(...); 另外,可采取以下措施避免回调丢失:
方式一:避免复用HttpRequest对象。每次请求都新建一个http.createHttp()实例,并在请求结束后调用destroy()释放资源。
方式二:使用once('headersReceive')替代on('headersReceive')。如果只需要一次回调,可改用once避免重复订阅带来的潜在问题。
方式三:增加错误处理与日志。在requestInStream的catch中打印错误码和错误信息,判断是否因超时或网络异常导致:
httpRequest.requestInStream("EXAMPLE_URL", options)
.then(...)
.catch((err: BusinessError) => {
console.error(`requestInStream ERROR : err = ${JSON.stringify(err)}`);
});