ohos.request (上传下载) 能力如何调试
我使用的是模拟器,配置了抓包工具Charles,普通请求都能抓包获取;
开发图片上传功能过程中:
let uploadTask: request.UploadTask;
let uploadConfig: request.UploadConfig = {
url: 'http://www.huawei.com', // 需要手动将 url 替换为真实服务器的 HTTP 协议地址
header: { 'Accept': '*/*' },
method: "POST",
files: [{ filename: "test", name: "test", uri: "internal://cache/test.jpg", type: "jpg" }],
data: [{ name: "name123", value: "123" }],
};
try {
request.uploadFile(getContext(), uploadConfig).then((data: request.UploadTask) => {
uploadTask = data;
}).catch((err: BusinessError) => {
console.error(`Failed to request the upload. Code: ${err.code}, message: ${err.message}`);
});
} catch (err) {
console.error(`Failed to request the upload. err: ${JSON.stringify(err)}`);
}
抓包工具没有抓到对应的链接,这个是在模拟器上无法调试,还是配置错误,为什么触发不了请求;
断点调试
uploadTask.on('complete', upCompleteCallback);
uploadTask.on('progress', upProgressCallback);
建议使用真机进行测试,以真机为主,模拟器抓包抓不到,大概率是配置的问题,参考下该链接进行排查:https://gitee.com/openharmony/applications\_app\_samples/tree/master/code/BasicFeature/Connectivity/UploadAndDownLoad\#/openharmony/applications\_app\_samples/blob/master/code/BasicFeature/Connectivity/UploadAndDownLoad/features/uploadanddownload/src/main/ets/upload/RequestUpload.ets