HarmonyOS 鸿蒙Next Http发起的requestInStream流式请求,dataReceiveProgress不执行
HarmonyOS 鸿蒙Next Http发起的requestInStream流式请求,dataReceiveProgress不执行 我发起了一个流式请求。(获取摄像头的数据,后用image显示)
发现订阅走了 headersReceive 和 dataReceive;不走dataEnd和dataReceiveProgress; dataReceive拿到数据后用image.createPixelMap 转出图片,但是出现彩色马赛克。
这个请求在网页请求是可以出现画面的。如图
let streamUrlStr = "http://192.168.0.111:8080/?action=stream";
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
// 用于订阅HTTP流式响应数据接收事件
let res = new ArrayBuffer(0);
// 用于订阅HTTP响应头事件
httpRequest.on('headersReceive', (header: Object) => {
console.info('xx00-header: ' + JSON.stringify(header));
});
httpRequest.on('dataReceive', (data: ArrayBuffer) => {
const newRes = new ArrayBuffer(res.byteLength + data.byteLength);
const resView = new Uint8Array(newRes);
resView.set(new Uint8Array(res));
resView.set(new Uint8Array(data), res.byteLength);
res = newRes;
console.info('XXOO-res length: ' + res.byteLength);
console.info('xx00-data length: ' + data.byteLength);
// data 转图片类型 显示出来
let opts: image.InitializationOptions = {
// alphaType: 0,
size: { width: 100, height:50},// 1920 1080
pixelFormat: image.PixelMapFormat.RGBA_8888,
// editable: true
};
image.createPixelMap(newRes,opts).then((pix)=>{
this.mainImage = pix;
}).catch((error: BusinessError) => {
console.error(`Failed to create pixelmap. code is ${error.code}, message is ${error.message}`);
});
});
// 用于订阅HTTP流式响应数据接收完毕事件
httpRequest.on('dataEnd', () => {
console.info('xxoo-No more data in response, data receive end');
});
// 用于订阅HTTP流式响应数据接收进度事件
class Data {
receiveSize: number = 0;
totalSize: number = 0;
}
httpRequest.on('dataReceiveProgress', (data: Data) => {
console.log("xxoo-dataReceiveProgress receiveSize:" + data.receiveSize + ", totalSize:" + data.totalSize);
});
let streamInfo: http.HttpRequestOptions = {
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/octet-stream:ArrayBuffer'//'application/json'
},
// expectDataType: http.HttpDataType.STRING,// 可选,指定返回数据的类型
usingCache: false, // 可选,默认为true
priority: 1, // 可选,默认为1
connectTimeout: 60000, // 可选,默认为60000ms
readTimeout: 60000, // 可选,默认为60000ms。若传输的数据较大,需要较长的时间,建议增大该参数以保证数据传输正常终止
usingProtocol: http.HttpProtocol.HTTP1_1 // 可选,协议类型默认值由系统自动指定
}
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
httpRequest.requestInStream(streamUrlStr, streamInfo).then((data: number) => {
console.info("requestInStream OK!");
console.info('ResponseCode :' + JSON.stringify(data));
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 取消订阅HTTP流式响应数据接收事件
httpRequest.off('dataReceive');
// 取消订阅HTTP流式响应数据接收进度事件
httpRequest.off('dataReceiveProgress');
// 取消订阅HTTP流式响应数据接收完毕事件
httpRequest.off('dataEnd');
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest.destroy();
}).catch((err: Error) => {
console.info("requestInStream ERROR : err = " + JSON.stringify(err));
});
更多关于HarmonyOS 鸿蒙Next Http发起的requestInStream流式请求,dataReceiveProgress不执行的实战教程也可以访问 https://www.itying.com/category-93-b0.html
楼主你好,问题已反馈给相关内部人员分析查看
更多关于HarmonyOS 鸿蒙Next Http发起的requestInStream流式请求,dataReceiveProgress不执行的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
发起请求后打日志信息、画面如下:
12-04 23:43:49.229 21797-21797 A03d00/JSAPP I xx00-header: {"cache-control":"no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0","connection":"close","content-type":"multipart/x-mixed-replace;boundary=boundarydonotcross","expires":"Mon, 3 Jan 2000 12:34:56 GMT","pragma":"no-cache","server":"Camera WLAN configuration"}
12-04 23:43:49.229 21797-21797 A03d00/JSAPP I XXOO-res length: 22
12-04 23:43:49.229 21797-21797 A03d00/JSAPP I xx00-data length: 22
12-04 23:43:49.229 21797-21828 C02b61/PixelMap E colors length: 22, offset: 0, width: 100 is invalid
12-04 23:43:49.229 21797-21797 C02b61/PixelMapNapi E NewPixelNapiInstance pixelMap is nullptr
12-04 23:43:49.229 21797-21797 C02b61/PixelMapNapi E New instance could not be obtained
12-04 23:43:49.229 21797-21797 A03d00/JSAPP E Failed to create pixelmap. code is undefined, message is undefined
12-04 23:43:49.230 21797-21797 A03d00/JSAPP I XXOO-res length: 1470
12-04 23:43:49.230 21797-21797 A03d00/JSAPP I xx00-data length: 1448
12-04 23:43:49.230 21797-21827 C02b61/PixelMap E colors length: 1470, offset: 0, width: 100 is invalid
12-04 23:43:49.230 21797-21797 C02b61/PixelMapNapi E NewPixelNapiInstance pixelMap is nullptr
12-04 23:43:49.230 21797-21797 C02b61/PixelMapNapi E New instance could not be obtained
12-04 23:43:49.230 21797-21797 A03d00/JSAPP E Failed to create pixelmap. code is undefined, message is undefined
12-04 23:43:49.230 21797-21797 A03d00/JSAPP I XXOO-res length: 7230
12-04 23:43:49.230 21797-21797 A03d00/JSAPP I xx00-data length: 5760
12-04 23:43:49.230 21797-21797 A03d00/JSAPP I XXOO-res length: 11606
针对帖子标题中提到的HarmonyOS(鸿蒙)Next Http发起的requestInStream流式请求中dataReceiveProgress
不执行的问题,这里提供可能的解决方案:
在鸿蒙系统中,如果dataReceiveProgress
回调不执行,首先检查以下几点:
-
确保回调已正确设置:在发起流式请求时,确保已经正确设置了
dataReceiveProgress
回调方法。检查代码中是否有遗漏或错误地设置回调。 -
检查数据流状态:确认服务器是否正在发送数据流。如果服务器没有发送数据或数据发送中断,
dataReceiveProgress
将不会触发。 -
网络状态:检查设备的网络连接状态。不稳定的网络连接可能导致数据接收中断,从而影响回调的执行。
-
异常处理:检查代码中是否有异常处理逻辑,确保在数据接收过程中出现的任何异常都被正确捕获和处理。
-
日志输出:增加日志输出,记录关键步骤和变量状态,这有助于诊断问题所在。
如果以上步骤都确认无误但问题依旧存在,可能是由于鸿蒙系统或相关库的bug导致的。此时,建议查阅鸿蒙系统的官方文档或更新日志,看是否有相关的已知问题或修复补丁。
如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html,