请求服务器图片资源刷新卡片

卡片中经常需要更新数据,如天气中更新温度等,都需要请求服务器资源更新目前的温度,气候等,接受服务器的资源如图片等进行重新渲染。

HarmonyOS
2024-05-21 20:47:27
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
wngsheng

使用的核心API

downloadFile(context: BaseContext, config: DownloadConfig): Promise
updateForm(formId: string, formBindingData: formBindingData.FormBindingData): Promise
postCardAction(component: Object, action: Object): void
  • 1.
  • 2.
  • 3.

核心代码解释

卡片的渲染页面中增加一个按钮来触发刷新操作,postCardAction为组件的能力请到指南中卡片部分参考,其余两个接口可在API文档查阅,通过postCardAction发送一个messgae,该接口会触发卡片ability的onFormEvent生命周期,下载和刷新操作在该生命周期进行。以下按流程分享源码:

核心代码如下:

//pages/WidgetCard.ets 
let storage = new LocalStorage(); 
@Entry(storage) 
@Component 
struct WidgetCard { 
  @LocalStorageProp('text') text: string = '加载中...'; 
  @LocalStorageProp('loaded') loaded: boolean = false; 
  @LocalStorageProp('imgName') imgName: string = 'name'; 
 
  build() { 
    Column() { 
      Text(this.text) 
        .fontSize('12vp') 
        .textAlign(TextAlign.Center) 
        .width('100%') 
        .height('30%') 
        .margin({bottom:20}) 
      Row() { 
        if (this.loaded) { 
          Image('memory://' + this.imgName) 
            .width('30%') 
            .height('30%') 
            .margin('5%') 
        } else { 
          Image($r("app.media.startIcon")) 
            .width('20%') 
            .height('20%') 
            .margin('5%') 
        } 
      }.alignItems(VerticalAlign.Center) 
      .justifyContent(FlexAlign.Center) 
      Button('刷新') 
        .height('15%') 
        .onClick(() => { 
          //发送message,触发卡片的onFormEvent生命周期 
          postCardAction(this, { 
            'action': 'message', 
            'params': { 
              'info': 'refreshImage' 
            } 
          }); 
        }) 
    } 
    .width('100%').height('100%') 
    .alignItems(HorizontalAlign.Center) 
    .padding('5%') 
  } 
}
//entryformability/EntryFormAbility 
import formBindingData from '@ohos.app.form.formBindingData'; 
import formProvider from '@ohos.app.form.formProvider'; 
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility'; 
import request from '@ohos.request'; 
import fs from '@ohos.file.fs'; 
import Base from '@ohos.base'; 
import fileFs from '@ohos.file.fs'; 
import Want from '@ohos.app.ability.Want'; 
import formInfo from '@ohos.app.form.formInfo'; 
 
export default class EntryFormAbility extends FormExtensionAbility { 
  onAddForm(want: Want) { 
    // 假设在当前卡片应用的tmp目录下有一个本地图片:head.PNG 
    let tempDir = this.context.getApplicationContext().tempDir; 
    // 打开本地图片并获取其打开后的fd 
    let file: fileFs.File; 
    let imgBear: Record<string, number>; 
    try { 
      file = fs.openSync(tempDir + '/' + 'icon.png'); 
      imgBear = { 
        'imgBear': file.fd 
      } 
    } catch (e) { 
      console.error(`openSync failed: ${JSON.stringify(e as Base.BusinessError)}`); 
    } 
    class FormDataClass{ 
      text: string = 'Image: Bear' 
      imgName: string = 'imgBear' 
      loaded: boolean = true 
      formImages: Record<string, number> = imgBear 
    } 
    let formData = new FormDataClass(); 
 
    // 将fd封装在formData中并返回至卡片页面 
    return formBindingData.createFormBindingData(formData); 
  } 
 
  onCastToNormalForm(formId: string) { 
    // Called when the form provider is notified that a temporary form is successfully 
    // converted to a normal form. 
  } 
 
  onUpdateForm(formId: string) { 
    // Called to notify the form provider to update a specified form. 
  } 
 
  onChangeFormVisibility(newStatus: Record<string, number>) { 
    // Called when the form provider receives form events from the system. 
  } 
 
  onFormEvent(formId: string, message: string) { 
    let param: Record<string, string> = { 
      'text': '刷新中...' 
    }; 
    let formInfo: formBindingData.FormBindingData =formBindingData.createFormBindingData(param); 
    formProvider.updateForm(formId, formInfo); 
    // 注意:FormExtensionAbility在触发生命周期回调时被拉起,仅能在后台存在5秒 
    // 建议下载能快速下载完成的小文件,如在5秒内未下载完成,则此次网络图片无法刷新至卡片页面上 
    let netFile = 'https://piccpndali.v.myalicdn.com/pic/cctv2_2.png'; // 需要在此处使用真实的网络图片下载链接 
    let tempDir = this.context.getApplicationContext().tempDir; 
    let fileName = 'file' + Date.now(); 
    let tmpFile = tempDir + '/' + fileName+'.png'; 
    console.info('ArkTSCard download complete:'); 
    request.downloadFile(this.context, { 
      url: netFile, filePath: tmpFile, enableMetered: true, enableRoaming: true 
    }).then((task) => { 
      task.on('complete', () => { 
        console.info('ArkTSCard download complete:' + tmpFile); 
        let file: fileFs.File; 
        let imgBear: Record<string, number>; 
        try { 
          file = fs.openSync(tmpFile); 
          imgBear = { 
            'imgBear': file.fd 
          } 
          console.info('ArkTSCard download file.fd:' + file.fd); 
        } catch (e) { 
          console.error(`openSync failed: ${JSON.stringify(e as Base.BusinessError)}`); 
        } 
        class FormDataClass{ 
          text: string = 'Image:' + fileName 
          imgName: string = 'imgBear' 
          loaded: boolean = true 
          formImages: Record<string, number> = imgBear 
        } 
        let formData = new FormDataClass(); 
        console.info('FormAbility updateForm datainfo:{'+formData.loaded+' '+ formData.text + ' ' +formData.formImages +' ' +formData.imgName +'}.'); 
        let formInfo = formBindingData.createFormBindingData(formData); 
        formProvider.updateForm(formId, formInfo).then(() => { 
          console.info('FormAbility updateForm success.'); 
        }).catch((error: Base.BusinessError) => { 
          console.error('FormAbility updateForm failed: ' + JSON.stringify(error)); 
        }); 
      }) 
      task.on('fail', (err: number) => { 
        console.info('ArkTSCard download task failed. Cause:' + err); 
        let param: Record<string, string> = { 
          'text': '刷新失败' 
        }; 
        let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(param); 
        formProvider.updateForm(formId, formInfo); 
      }); 
    }).catch((err: Base.BusinessError) => { 
      console.error('Failed to request the download. Cause: ' + JSON.stringify(err)); 
    }); 
  } 
 
  onRemoveForm(formId: string) { 
    // Called to notify the form provider that a specified form has been destroyed. 
  } 
 
  onAcquireFormState(want: Want) { 
    // Called to return a {@link FormState} object. 
    return formInfo.FormState.READY; 
  } 
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.

总结:

主要难点在于:资源文件的下载和卡片数据的封装。

实现效果

注明适配的版本信息

IDE:DevEco    Studio 4.0.3.600

SDK:HarmoneyOS    4.0.10.11

分享
微博
QQ
微信
回复
2024-05-22 16:52:01
相关问题
HarmonyOS 上传图片服务器
447浏览 • 1回复 待解决
HarmonyOS HTTPS请求如何验证服务器证书
1503浏览 • 1回复 待解决
HarmonyOS 选择图片上传到服务器的demo
714浏览 • 1回复 待解决
HarmonyOS 将相册中的图片上传到服务器
1231浏览 • 1回复 待解决
centos服务器系统宕机
2623浏览 • 1回复 待解决
HarmonyOS 文件上传服务器问题
983浏览 • 1回复 待解决
HarmonyOS 卡片更新图片刷新
661浏览 • 1回复 待解决
mysql shell 如何查看服务器状态?
4660浏览 • 1回复 待解决