OpenHarmony趣味应用 OpenHarmony藏头诗应用

网友投稿 257 2022-11-11

OpenHarmony趣味应用 OpenHarmony藏头诗应用

我要实现的功能主要有:

生成藏头诗,

生成整首诗,

你能学到的有:

网络请求

可滚动组件

状态管理

常用组件

常用属性

修改应用名称和图标

在Config.json添加权限等

用到的接口:

接口:

请求方式:

Get

接口:

apipost请求测试:

如何创建应用在这里不做解释。

首先预览一下应用

注意点:

"deviceConfig": {

"default": {

"network": {

}

}

},

使用网络请求在config.json添加权限:

"reqPermissions": [

{

"name": "ohos.permission.INTERNET"

}

],

完整代码:

@Entry

@Component

struct Index {

@State tibetanInput: string = "跟着坚果学鸿蒙";

@State wholeContent: string = "";

@State wholeInput: string = "跟着坚果学鸿蒙";

private scroller: Scroller = new Scroller()

console.info('关闭')

}

build() {

Scroll(this.scroller) {

Column({ space: 10 }) {

Text($r("app.string.title"))

.fontSize(26)

.fontWeight(FontWeight.Bold)

.align(Alignment.Start)

.margin({ top: 20 })

TextInput({ placeholder: '请输入要生成的内容', })

.fontSize(36)

.enterKeyType(EnterKeyType.Go)

.onChange((value) => {

this.tibetanInput = value;

})

.height(80)

.margin({

top: 40,

left: 16,

right: 16

})

.onClick(() => {

this.TibetanRequest();

})

Text(this.tibetanContent).fontSize(26).fontColor(Color.Orange)

TextInput({ placeholder: '请输入要生成的内容', })

.fontSize(36)

.enterKeyType(EnterKeyType.Go)

.onChange((value) => {

this.wholeInput = value;

})

.height(80)

.margin({

left: 16,

right: 16

})

Button("生成整首诗").backgroundColor(Color.Green)

.onClick(() => {

this.wholePoemRequest();

})

Text(this.wholeContent).fontSize(24).fontColor(Color.Orange)

}

.padding(10)

}

}

//藏头诗接口

private TibetanRequest() {

{

method: RequestMethod.RequestMethod.GET,

connectTimeout: 15000,

},

(error, data) => {

if (error) {

console.log("error code: " + error.code + ", msg: " + error.message)

} else {

let code = data.responseCode

if (ResponseCode.ResponseCode.OK == code) {

this.tibetanContent = data.result.toString();

let header = JSON.stringify(data.header);

console.log("result: " + this.tibetanContent);

console.log("header: " + header);

} else {

console.log("response code: " + code);

}

}

}

);

}

//整首诗接口

private wholePoemRequest() {

{

method: RequestMethod.RequestMethod.GET,

readTimeout: 15000,

connectTimeout: 15000,

},

(error, data) => {

if (error) {

console.log("error code: " + error.code + ", msg: " + error.message)

} else {

let code = data.responseCode

if (ResponseCode.ResponseCode.OK == code) {

this.wholeContent = data.result.toString();

let header = JSON.stringify(data.header);

console.log("result: " + this.wholeContent);

console.log("header: " + header);

} else {

console.log("response code: " + code);

}

}

}

);

}

}

发起网络请求

import

from

;

let

=

.

createHttp

();

无 RequestOptions 参数请求

request() 方法默认采用 get 方式请求。

上述代码,重点是通过调用HTTP的AI接口,来获取生成接口返回的诗的内容,并显示在应用界面上。

修改应用描述信息

默认的应用描述信息,集中在config.json文件中。

修改string.json内容如下:

"srcLanguage": "ets",

"srcPath": "MainAbility",

"icon": "$media:icon", //应用图标

"description": "$string:desc",

"label": "$string:title", //应用名称

"type": "page",

"visible": true,

"launchType": "standard"

这么有趣的应用就这样完成了,比起js开发方式,eTS是不是更为简单呢。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HDI的IPC模式具体实现方法和驱动框架能力
下一篇:Nginx +keepalived+varnish+lamp实现高可用、负载均衡集群
相关文章

 发表评论

暂时没有评论,来抢沙发吧~