HarmonyOS request.agent.show返回的参数中extras参数为空?

代码:

const taskInfo = await request.agent.show(String(taskId)) as request.agent.TaskInfo 
console.log('taskInfo', taskInfo) 

其中返回的Progress中extras为空。

阅读 437
1 个回答

参考demo:

import common from '@ohos.app.ability.common';
import request from '@ohos.request';
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';

let context = getContext(this) as common.UIAbilityContext;
@Entry
@Component
struct Page1 {
  @State componentSnapshotPixelMap: image.PixelMap | null = null;

  build() {
    Row() {
      Column({ space: 5 }) {
        Button("DownLoad").onClick(() => {
          DownLoad()
        })

      }.width('100%')
    }.height('100%')
  }
}

function DownLoad() {
  try {
    let filePath = context.cacheDir + '/' + new Date().getTime() + '_xx.jpg'
    request.downloadFile(context, {
      url: 'xxx',
      filePath: filePath
    }).then((data: request.DownloadTask) => {
      let downloadTask: request.DownloadTask = data;
      let completeCallback = async () => {
        const downloadInfo = await downloadTask.getTaskInfo()
        const taskId = downloadInfo.downloadId
        const header = await getMoreTaskInfo(taskId) as Record<string, string>
        console.info("header:" + JSON.stringify(header))
      }
      downloadTask.on('complete', completeCallback);
    })
  } catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
  }
}

async function getMoreTaskInfo(taskId: number) {
  const taskInfo = await request.agent.show(String(taskId)) as request.agent.TaskInfo
  console.info("taskInfo:" + JSON.stringify(taskInfo))
  return taskInfo.progress.extras
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进