HarmonyOS 鸿蒙Next AI作诗eTS版

HarmonyOS 鸿蒙Next AI作诗eTS版 今天我们将做一个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);
        }

      }
    }

  );
}

上述代码,重点是通过调用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是不是更为简单呢。


更多关于HarmonyOS 鸿蒙Next AI作诗eTS版的实战教程也可以访问 https://www.itying.com/category-93-b0.html

10 回复

cke_143.png

更多关于HarmonyOS 鸿蒙Next AI作诗eTS版的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


不错哦,

基本信息

深色代码主题

这个大佬咋在这发布作品啦,去活动贴参加赢礼品呀

[https://developer.huawei.com/consumer/cn/forum/topic/0204926818814690599?fid=23]

有人说不会添加权限,那我就把完整的权限也贴一下吧

config.json
```javascript
{
  "app": {
    "bundleName": "com.jianguo.myapplication",
    "vendor": "example",
    "version": {
      "code": 1000000,
      "name": "1.0.0"
    }
  },
  "deviceConfig": {
    "default": {
      "network": {
        "cleartextTraffic": true //默认http,加这个可以使用https
      }
    }
  },
  "module": {
    "package": "com.example.entry",
    "name": ".entry",
    "mainAbility": ".MainAbility",
    "deviceType": [
      "phone"
    ],
    "distro": {
      "deliveryWithInstall": true,
      "moduleName": "entry",
      "moduleType": "entry",
      "installationFree": false
    },
    "reqPermissions": [
      {
        "name": "ohos.permission.INTERNET" //权限
      }
    ],
    "abilities": [
      {
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ],
        "orientation": "unspecified",
        "formsEnabled": false,
        "name": ".MainAbility",
        "srcLanguage": "ets",
        "srcPath": "MainAbility",
        "icon": "$media:icon",
        "description": "$string:desc",
        "label": "$string:title",
        "type": "page",
        "visible": true,
        "launchType": "standard"
      }
    ],
    "js": [
      {
        "mode": {
          "syntax": "ets",
          "type": "pageAbility"
        },
        "pages": [
          "pages/index"
        ],
        "name": ".MainAbility",
        "window": {
          "designWidth": 720,
          "autoDesignWidth": false
        }
      }
    ]
  }
}

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

贴个完整代码似乎还不够哦,搞不出来,权限那里最好也贴出来,新手不懂啊,之前用java设过权限,这里是eTS,权限那里用户场景没有能力设置啊

权限就一个,那我加一下吧,

还可以

针对帖子标题“HarmonyOS 鸿蒙Next AI作诗eTS版”的问题,以下是我根据要求给出的回答:

HarmonyOS 鸿蒙Next AI作诗eTS版是鸿蒙系统中利用eTS(Extensible TypeScript)语言开发的一个AI作诗应用。eTS是鸿蒙系统为开发者提供的一种新的声明式编程语言,旨在简化开发过程,提高开发效率。

在HarmonyOS鸿蒙系统中,eTS被广泛应用于开发各种应用,包括AI作诗这样的创新应用。AI作诗应用通过训练好的AI模型,能够根据用户输入的主题、风格等要求,自动生成符合要求的诗句。

对于“HarmonyOS 鸿蒙Next AI作诗eTS版”的具体实现,它可能涉及到了鸿蒙系统的AI框架、eTS编程语言的特性以及相关的UI设计。开发者需要利用鸿蒙系统提供的API和工具,将AI模型集成到应用中,并实现用户交互界面。

如果你在开发或使用过程中遇到了问题,可能是由多种原因导致的。建议检查代码逻辑、依赖库版本、系统兼容性等方面。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部