提交 216f5a67 编写于 作者: C chenyuyan

idl示例代码增加map和array数据类型

Signed-off-by: Nchenyuyan <chenyuyan3@huawei.com>
Change-Id: I4e12a0fd5a0ba057c8003fa9cdce9ba56b8b3705
上级 d653d420
......@@ -158,9 +158,9 @@ OpenHarmony IDL容器数据类型与Ts数据类型、C++数据类型的对应关
> **注意**:请保证使用最新版的SDK,版本老旧可能导致部分语句报错。
若不存在,可对应版本前往[docs仓版本目录](https://gitee.com/openharmony/docs/tree/master/zh-cn/release-notes)下载SDK包,以[3.2Beta3版本](../../release-notes/OpenHarmony-v3.2-beta3.md#%E4%BB%8E%E9%95%9C%E5%83%8F%E7%AB%99%E7%82%B9%E8%8E%B7%E5%8F%96)为例,可通过镜像站点获取。
若不存在,可对应版本前往[docs仓版本目录](https://gitee.com/openharmony/docs/tree/master/zh-cn/release-notes)下载SDK包,以[3.2Beta3版本](https://gitee.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v3.2-beta3.md)为例,可通过镜像站点获取。
关于如何替换DevEco Studio的SDK包具体操作,参考[full-SDK替换指南](../quick-start/full-sdk-switch-guide.md#full-sdk%E6%9B%BF%E6%8D%A2%E6%8C%87%E5%8D%97)中的替换方法。
关于如何替换DevEco Studio的SDK包具体操作,参考[full-SDK替换指南](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/full-sdk-compile-guide.md)中的替换方法。
得到idl工具的可执行文件后,根据具体场景进行后续开发步骤。
......@@ -176,6 +176,8 @@ OpenHarmony IDL容器数据类型与Ts数据类型、C++数据类型的对应关
interface OHOS.IIdlTestService {
int TestIntTransaction([in] int data);
void TestStringTransaction([in] String data);
void TestMapTransaction([in] Map<int, int> data);
int TestArrayTransaction([in] String[] data);
}
```
......@@ -203,6 +205,8 @@ OpenHarmony IDL工具生成的Stub类是接口类的抽象实现,并且会声
```ts
import {testIntTransactionCallback} from "./i_idl_test_service";
import {testStringTransactionCallback} from "./i_idl_test_service";
import {testMapTransactionCallback} from "./i_idl_test_service";
import {testArrayTransactionCallback} from "./i_idl_test_service";
import IIdlTestService from "./i_idl_test_service";
import rpc from "@ohos.rpc";
......@@ -211,8 +215,8 @@ export default class IdlTestServiceStub extends rpc.RemoteObject implements IIdl
super(des);
}
async onRemoteRequestEx(code: number, data, reply, option): Promise<boolean> {
console.log("onRemoteRequestEx called, code = " + code);
async onRemoteMessageRequest(code: number, data, reply, option): Promise<boolean> {
console.log("onRemoteMessageRequest called, code = " + code);
switch(code) {
case IdlTestServiceStub.COMMAND_TEST_INT_TRANSACTION: {
let _data = data.readInt();
......@@ -231,6 +235,29 @@ export default class IdlTestServiceStub extends rpc.RemoteObject implements IIdl
});
return true;
}
case IdlTestServiceStub.COMMAND_TEST_MAP_TRANSACTION: {
let _data = new Map();
let _dataSize = data.readInt();
for (let i = 0; i < _dataSize; ++i) {
let key = data.readInt();
let value = data.readInt();
_data.set(key, value);
}
this.testMapTransaction(_data, (errCode) => {
reply.writeInt(errCode);
});
return true;
}
case IdlTestServiceStub.COMMAND_TEST_ARRAY_TRANSACTION: {
let _data = data.readStringArray();
this.testArrayTransaction(_data, (errCode, returnValue) => {
reply.writeInt(errCode);
if (errCode == 0) {
reply.writeInt(returnValue);
}
});
return true;
}
default: {
console.log("invalid request code" + code);
break;
......@@ -241,17 +268,23 @@ export default class IdlTestServiceStub extends rpc.RemoteObject implements IIdl
testIntTransaction(data: number, callback: testIntTransactionCallback): void{}
testStringTransaction(data: string, callback: testStringTransactionCallback): void{}
testMapTransaction(data: Map<number, number>, callback: testMapTransactionCallback): void{}
testArrayTransaction(data: string[], callback: testArrayTransactionCallback): void{}
static readonly COMMAND_TEST_INT_TRANSACTION = 1;
static readonly COMMAND_TEST_STRING_TRANSACTION = 2;
static readonly COMMAND_TEST_MAP_TRANSACTION = 3;
static readonly COMMAND_TEST_ARRAY_TRANSACTION = 4;
}
```
开发者需要继承.idl文件中定义的接口类并实现其中的方法。在本示例中,我们继承了IdlTestServiceStub接口类并实现了其中的testIntTransaction和testStringTransaction方法。具体的示例代码如下:
开发者需要继承.idl文件中定义的接口类并实现其中的方法。在本示例中,我们继承了IdlTestServiceStub接口类并实现了其中的testIntTransaction、testStringTransaction、testMapTransaction和testArrayTransaction方法。具体的示例代码如下:
```ts
import {testIntTransactionCallback} from "./i_idl_test_service"
import {testStringTransactionCallback} from "./i_idl_test_service"
import {testMapTransactionCallback} from "./i_idl_test_service";
import {testArrayTransactionCallback} from "./i_idl_test_service";
import IdlTestServiceStub from "./idl_test_service_stub"
......@@ -265,6 +298,14 @@ class IdlTestImp extends IdlTestServiceStub {
{
callback(0);
}
testMapTransaction(data: Map<number, number>, callback: testMapTransactionCallback): void
{
callback(0);
}
testArrayTransaction(data: string[], callback: testArrayTransactionCallback): void
{
callback(0, 1);
}
}
```
......@@ -320,11 +361,28 @@ function callbackTestStringTransaction(result: number): void {
}
}
function callbackTestMapTransaction(result: number): void {
if (result == 0) {
console.log('case 3 success');
}
}
function callbackTestArrayTransaction(result: number, ret: number): void {
if (result == 0 && ret == 124) {
console.log('case 4 success');
}
}
var onAbilityConnectDone = {
onConnect:function (elementName, proxy) {
let testProxy = new IdlTestServiceProxy(proxy);
let testMap = new Map();
testMap.set(1, 1);
testMap.set(1, 2);
testProxy.testIntTransaction(123, callbackTestIntTransaction);
testProxy.testStringTransaction('hello', callbackTestStringTransaction);
testProxy.testMapTransaction(testMap, callbackTestMapTransaction);
testProxy.testArrayTransaction(['1','2'], callbackTestMapTransaction);
},
onDisconnect:function (elementName) {
console.log('onDisconnectService onDisconnect');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册