OpenHarmony趣味应用 OpenHarmony藏头诗应用

今天我们将做一个openharmony趣味应用——openharmony藏头诗应用,是通过ai接口来做。通过调用指定的ai接口来做,接口会返回藏头诗或者继续完成诗的后面几句。
我要实现的功能主要有: 生成藏头诗,
生成整首诗,
你能学到的有: 网络请求
可滚动组件
状态管理
常用组件
常用属性
修改应用名称和图标
在config.json添加权限等
用到的接口:  
接口:
https://py.myie9.com/hidepoem/坚果
请求方式:
get
apipost请求测试
接口:
https://py.myie9.com/xuxietest/汗滴禾下土
apipost请求测试:
如何创建应用在这里不做解释。
首先预览一下应用
注意点: 允许https需要添加下面的配置
deviceconfig: {
    default: {
      network: {
        cleartexttraffic: true
      }
    }
  },
使用网络请求在config.json添加权限:
 reqpermissions: [
      {
        name: ohos.permission.internet
      }
    ],
完整代码: import http from '@ohos.net.http';
import requestmethod from '@ohos.net.http';
import responsecode from '@ohos.net.http';


@entry
@component
struct index {
  @state tibetancontent: string = 坚果的小跟班;
  @state tibetaninput: string = 跟着坚果学鸿蒙;
  @state wholecontent: string = ;
  @state wholeinput: string = 跟着坚果学鸿蒙;
  private scroller: scroller = new scroller()



  oncancel() {
    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
          })

        button(生成藏头诗).backgroundcolor(color.pink)
          .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() {
    let httprequest = http.createhttp();
    httprequest.request(
      https://py.myie9.com/hidepoem/ + this.tibetaninput,
      {
        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.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() {
    let httprequest = http.createhttp();
    httprequest.request(
      https://py.myie9.com/xuxietest/ + this.wholeinput,
      {
        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);
          }
        }
      }
    );
  }
}
发起网络请求 使用 @ohos.net.http 模块发起网络请求分为以下步骤:
引入http模块
import
http
from
'@ohos.net.http'
;
创建一个httprequest
let
httprequest
=
http
.
createhttp
();
发起http请求
httprequest 提供了两种 request() 方法进行网络请求,分别是无 requestoptions 参数的请求和有 requestoptions 参数的请求。分别介绍如下:
无 requestoptions 参数请求
  //藏头诗接口
  private tibetanrequest() {
    let httprequest = http.createhttp();
    httprequest.request(
      https://py.myie9.com/hidepoem/ + this.tibetaninput,
      {
        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.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);
          }

        }
      }

    );
  } 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是不是更为简单呢。

国内首家能够生产最大尺寸电容触摸屏的高新技术企业
电力稳压调控装置的特点
新品上市 | 5mm,极致超薄,鸿雁博悦系列开关重磅来袭
用于航空航天和国防的 GaN 和 SiC 解决方案
VR技术在医疗行业的应用将造福广大患者
OpenHarmony趣味应用 OpenHarmony藏头诗应用
信创交换机:连接未来创新发展的动力引擎
中国拟制定无线充电技术标准,引领快充行业新潮流
二波段四灯直流机的制作
保隆科技获得国家级专精特新小巨人认定
一文了解浪涌
吃个披萨就能理解云计算中IaaS、PaaS和SaaS
开关电容转换器简化了从1S到2S电池架构的迁移
中国联通与中国邮政签署战略协议,将在五大领域深化合作
开关电源如何降低纹波?
单片机项目中LED的重要性
苹果成功在香港注册了“Apple Card”及“Apple Cash”两项新商标
安防与智慧城市之间有着怎样的瓜葛呢?
常用汽车线束设计软件有哪些
MINITÜB 的UR机器人解决方案