谷动谷力

 找回密码
 立即注册
查看: 2777|回复: 0
打印 上一主题 下一主题
收起左侧

OpenHarmony 趣味应用:藏头诗小程序

[复制链接]
跳转到指定楼层
楼主
发表于 2022-7-15 09:46:57 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
OpenHarmony 趣味应用:藏头诗小程序


今天我们将做一个OpenHarmony趣味应用——OpenHarmony藏头诗应用,是通过AI接口来做。通过调用指定的AI接口来做,接口会返回藏头诗或者继续完成诗的后面几句。

我要实现的功能主要有:
  • 生成藏头诗
  • 生成整首诗


你能学到的有:
  • 网络请求
  • 可滚动组件
  • 状态管理
  • 常用组件
  • 常用属性
  • 修改应用名称和图标
  • 在Config.json添加权限等


用到的接口
接口:
https://py.myie9.com/hidepoem/坚果
(左右移动查看全部内容)

请求方式:
Get
apipost请求测试


接口:
https://py.myie9.com/xuxietest/汗滴禾下土
(左右移动查看全部内容)

apipost请求测试:


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

首先预览一下应用


注意点:

允许https需要添加下面的配置
  1. <blockquote>"deviceConfig": {
复制代码


使用网络请求在config.json添加权限:
  1. "reqPermissions": [
  2.       {
  3.            "name": "ohos.permission.INTERNET"
  4.       }
  5.     ],
复制代码


完整代码:
  1. import http from '@ohos.net.http';
  2. import RequestMethod from '@ohos.net.http';
  3. import ResponseCode from '@ohos.net.http';

  4. @Entry
  5. @Component
  6. struct Index {
  7.   @State tibetanContent: string = "坚果的小跟班";
  8.   @State tibetanInput: string = "跟着坚果学鸿蒙";
  9.   @State wholeContent: string = "";
  10.   @State wholeInput: string = "跟着坚果学鸿蒙";
  11.   private scroller: Scroller = new Scroller()

  12.   onCancel() {
  13.     console.info('关闭')
  14.   }

  15.   build() {
  16.     Scroll(this.scroller) {
  17.       Column({ space: 10 }) {
  18.         Text($r("app.string.title"))
  19.           .fontSize(26)
  20.           .fontWeight(FontWeight.Bold)
  21.           .align(Alignment.Start)
  22.           .margin({ top: 20 })

  23.         TextInput({ placeholder: '请输入要生成的内容', })
  24.           .fontSize(36)
  25.           .enterKeyType(EnterKeyType.Go)
  26.           .onChange((value) => {
  27.             this.tibetanInput = value;

  28.           })
  29.           .height(80)
  30.           .margin({
  31.             top: 40,
  32.             left: 16,
  33.             right: 16
  34.           })

  35.         Button("生成藏头诗").backgroundColor(Color.Pink)
  36.           .onClick(() => {
  37.                         this.TibetanRequest();

  38.           })
  39.         Text(this.tibetanContent).fontSize(26).fontColor(Color.Orange)
  40.         TextInput({ placeholder: '请输入要生成的内容', })
  41.           .fontSize(36)
  42.           .enterKeyType(EnterKeyType.Go)
  43.           .onChange((value) => {
  44.             this.wholeInput = value;

  45.           })
  46.           .height(80)
  47.           .margin({

  48.             left: 16,
  49.             right: 16
  50.           })
  51.         Button("生成整首诗").backgroundColor(Color.Green)
  52.           .onClick(() => {
  53.             this.wholePoemRequest();
  54.           })
  55.         Text(this.wholeContent).fontSize(24).fontColor(Color.Orange)
  56.       }
  57.       .padding(10)
  58.     }
  59.   }
  60.   //藏头诗接口
  61.   private TibetanRequest() {
  62.     let httpRequest = http.createHttp();
  63.     httpRequest.request(
  64.       "https://py.myie9.com/hidepoem/" + this.tibetanInput,
  65.       {
  66.         method: RequestMethod.RequestMethod.GET,
  67.         readTimeout: 15000,
  68.         connectTimeout: 15000,
  69.       },
  70.       (error, data) => {
  71.         if (error) {
  72.           console.log("error code: " + error.code + ", msg: " + error.message)
  73.         } else {
  74.           let code = data.responseCode
  75.           if (ResponseCode.ResponseCode.OK == code) {
  76.             this.tibetanContent = data.result.toString()
  77.             let header = JSON.stringify(data.header);
  78.             console.log("result: " + this.tibetanContent);
  79.             console.log("header: " + header);
  80.           } else {
  81.             console.log("response code: " + code);
  82.           }
  83.         }
  84.       }
  85.     );
  86.   }

  87.   //整首诗接口
  88.   private wholePoemRequest() {
  89.     let httpRequest = http.createHttp();
  90.     httpRequest.request(
  91.       "https://py.myie9.com/xuxietest/" + this.wholeInput,
  92.       {
  93.         method: RequestMethod.RequestMethod.GET,
  94.         readTimeout: 15000,
  95.         connectTimeout: 15000,
  96.       },
  97.       (error, data) => {
  98.         if (error) {
  99.           console.log("error code: " + error.code + ", msg: " + error.message)
  100.         } else {
  101.           let code = data.responseCode
  102.           if (ResponseCode.ResponseCode.OK == code) {
  103.             this.wholeContent = data.result.toString();
  104.             let header = JSON.stringify(data.header);
  105.             console.log("result: " + this.wholeContent);
  106.             console.log("header: " + header);
  107.           } else {
  108.             console.log("response code: " + code);
  109.           }
  110.         }
  111.       }
  112.     );
  113.   }
  114. }
复制代码


发起网络请求:
使用 `@ohos.net.http` 模块发起网络请求分为以下步骤:

引入http模块
import http from '@ohos.net.http';

创建一个httpRequest
let httpRequest = http.createHttp();

发起http请求
httpRequest`提供了两种 request() 方法进行网络请求,分别是无 RequestOptions参数的请求和有 RequestOptions参数的请求。分别介绍如下:

1、无 RequestOptions 参数请求
  1. //藏头诗接口
  2.      private TibetanRequest() {
  3.        let httpRequest = http.createHttp();
  4.        httpRequest.request(
  5.          "https://py.myie9.com/hidepoem/" + this.tibetanInput,
  6.          {
  7.            method: RequestMethod.RequestMethod.GET,
  8.            readTimeout: 15000,
  9.            connectTimeout: 15000,
  10.          },
  11.          (error, data) => {
  12.            if (error) {
  13.              console.log("error code: " + error.code + ", msg: " + error.message)
  14.            } else {
  15.              let code = data.responseCode
  16.              if (ResponseCode.ResponseCode.OK == code) {
  17.                this.tibetanContent = data.result.toString();
  18.                let header = JSON.stringify(data.header);
  19.                console.log("result: " + this.tibetanContent);
  20.                console.log("header: " + header);
  21.              } else {
  22.                console.log("response code: " + code);
  23.              }
  24.            }
  25.          }
  26.        );
  27.      }
复制代码

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

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

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



修改string.json内容如下:
  1. "srcLanguage": "ets",
  2.         "srcPath": "MainAbility",
  3.         "icon": "$media:icon", //应用图标
  4.         "description": "$string:desc",
  5.         "label": "$string:title", //应用名称
  6.         "type": "page",
  7.         "visible": true,
  8.         "launchType": "standard"
复制代码

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



+10
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-5-19 00:38 , Processed in 0.081129 second(s), 44 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表