一、创建项目
进入端云网站创建项目 AppGallery Connect (huawei.com)
数据处理默认选择中国
二、创建应用
三、在IDE中创建应用
项目目录如下
四、具体demo
1.在云端的cloudfunction中新建云函数
函数入参:
event:请求入参 http请求详细信息
context:上下文信息
callback:响应
logger:日志
云函数触发时机:HTTP请求、云数据库insert、云存储上传文件
函数体中的callback为回调函数,可自行封装响应信息
logger.info(event);//打印日志 日志级别与Java类似
如果函数书写完毕,可以右键cloudfunctions选择run cloud functions运行函数,确认函数是否有错误;
然后选择上传函数sync cloud functions
event对象内部结构:
可直接在AGC中直接点击“测试”进行函数测试
2.在侧端去调用云侧的函数
①合华为云签订协议
src/main/resources/rawfile/agconnect-services.json
②引入包
D:\Code\cound_myfirstApp\Application\entry\oh-package.json5
{
"license": "",
"devDependencies": {},
"author": "",
"name": "entry",
"description": "Please describe the basic information.",
"main": "",
"version": "1.0.0",
"dependencies": {
"@hw-agconnect/cloud": "^1.0.0", //云端的一些操作依赖
"@hw-agconnect/hmcore": "^1.0.0", //云端的连接
"@hw-agconnect/auth-component": "^1.0.0",
"long": "5.2.1"
}
}
补充
③在EntryAbility.ets引入依赖,初始化agc
onWindowStageCreate(windowStage: window.WindowStage): void {
//写法一
this.context.resourceManager.getRawFileContent("agconnect-services.json")
.then((value: Uint8Array) => {
let json: string = buffer.from(value as ArrayBufferLike).toString("utf8");
initialize(this.context, JSON.parse(json));
})
.catch((err: Object) => {
hilog.error(0x0000, 'testTag', 'Failed to initialize AGC SDK. Cause: %{public}s', JSON.stringify(err) ?? '');
});
}
async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
//写法二
const context = this.context
const value = await context.resourceManager.getRawFileContent("agconnect-services.json");//读取配置文件
let json: string = buffer.from(value).toString("utf8");//二进制转string
initialize(this.context, JSON.parse(json));//JSON.parse(json) 转json对象
}
import json from '../../resources/rawfile/agconnect-services.json'
async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
//写法三
initialize(this.context, json);
}
initialize(this.context, JSON.parse(json));
这个agc的初始化也可以在onCreate函数中进行,但注意try-catch
④页面验证
新建一个页面 xx.ets ,在build中新建一个button进行验证。
1)导入需要使用类
或者
2)将新建的ets配置到src/main/resources/base/profile/main_pages.json文件中
3)为了验证,可以在入口src/main/ets/entryability/EntryAbility.ets的src/main/ets/entryability/EntryAbility.ets:27中
windowStage.loadContent('pages/Index', (err, data) => { 将Index换成新建的页面
4)配置模拟器或者真机,点击run即可去验证