HarmonyOS 鸿蒙Next Asset(关键资产存储服务)查询不到存入的数据
HarmonyOS 鸿蒙Next Asset(关键资产存储服务)查询不到存入的数据 参考文档实现下面的demo,查询不到asset.Tag.SECRET数据:
import { util } from '@kit.ArkTS';
import { asset } from '@kit.AssetStoreKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
build() {
Column() {
Button('setAssetData').onClick(() => {
let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, stringToArray('demo_pwd11'));
attr.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label'));
try {
asset.addSync(attr);
console.error(`Suc to add Asset.`);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`);
}
})
Button('getAssetData').onClick(() => {
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
try {
let res: Array<asset.AssetMap> = asset.querySync(query);
for (let i = 0; i < res.length; i++) {
if (res[i] != null) {
const bs = res[i].get(asset.Tag.SECRET) as Uint8Array;
if (bs) {
console.error(`Suc to query is ${arrayToString(bs)}`);
} else {
console.error(`fail to query bs undefind!`);
}
}
}
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
}
})
}
.width('100%')
.height('100%')
.backgroundColor(0xDCDCDC)
}
}
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
function arrayToString(bs: Uint8Array): string {
let textDecoder = util.TextDecoder.create()
return textDecoder.decodeToString(bs)
}
更多关于HarmonyOS 鸿蒙Next Asset(关键资产存储服务)查询不到存入的数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
需要指定查询关键资产的返回类型,参考如下代码实现
import { util } from '@kit.ArkTS';
import { asset } from '@kit.AssetStoreKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
build() {
Column() {
Button('setAssetData').onClick(() => {
let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, stringToArray('demo_pwd11'));
attr.set(asset.Tag.ALIAS, stringToArray('demo_alias1'));
attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label1'));
try {
asset.addSync(attr);
console.error(`Suc to add Asset.`);
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`);
}
})
Button('getAssetData').onClick(() => {
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias1')); // 指定了关键资产别名,最多查询到一条满足条件的关键资产
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // 此处表示需要返回关键资产的所有信息,即属性+明文
try {
let res: Array<asset.AssetMap> = asset.querySync(query);
for (let i = 0; i < res.length; i++) {
if (res[i] != null) {
res[0].forEach((v, k)=>{
console.info(k+'<<<<'+v)
})
const bs = res[i].get(asset.Tag.SECRET) as Uint8Array;
console.info(bs+'<<<<<<<<');
if (bs) {
console.error(`Suc to query is ${arrayToString(bs)}`);
} else {
console.error(`fail to query bs undefind!`);
}
}
// parse the secret.
let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
// parse uint8array to string
let secretStr: string = arrayToString(secret);
console.info('<<>><<<<'+secretStr)
}
} catch (error) {
let err = error as BusinessError;
console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
}
})
}
.width('100%')
.height('100%')
.backgroundColor(0xDCDCDC)
}
}
function stringToArray(str: string): Uint8Array {
let textEncoder = new util.TextEncoder();
return textEncoder.encodeInto(str);
}
function arrayToString(bs: Uint8Array): string {
let textDecoder = util.TextDecoder.create()
return textDecoder.decodeToString(bs)
}
更多关于HarmonyOS 鸿蒙Next Asset(关键资产存储服务)查询不到存入的数据的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对HarmonyOS鸿蒙Next Asset(关键资产存储服务)查询不到存入数据的问题,可能的原因及解决方案如下:
首先,确认数据是否已正确存入Next Asset服务。检查存入数据的代码逻辑,确保数据格式、存储路径及权限设置均无误。若使用API进行数据存储,需核对API调用参数及响应结果,确认数据是否成功上传。
其次,检查查询数据的代码逻辑。确认查询API的使用是否正确,参数设置是否无误,以及查询条件是否与存入数据匹配。若查询条件设置不当,可能导致无法查询到期望的数据。
再者,考虑数据同步问题。若Next Asset服务存在数据同步机制,需确认数据同步是否已完成。在数据同步期间,可能暂时无法查询到最新存入的数据。
此外,还需检查网络连接状态。若网络连接不稳定或中断,可能导致数据存入或查询请求失败。
若以上步骤均无法解决问题,可能是Next Asset服务本身存在故障或维护状态。此时,建议联系鸿蒙官方客服进行进一步排查和处理。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html,