0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

OpenHarmony趣味應(yīng)用 OpenHarmony藏頭詩(shī)應(yīng)用

ArkUI詳解 ? 2022-07-13 09:20 ? 次閱讀

今天我們將做一個(gè)OpenHarmony趣味應(yīng)用——OpenHarmony藏頭詩(shī)應(yīng)用,是通過AI接口來做。通過調(diào)用指定的AI接口來做,接口會(huì)返回藏頭詩(shī)或者繼續(xù)完成詩(shī)的后面幾句。

我要實(shí)現(xiàn)的功能主要有:

生成藏頭詩(shī),

生成整首詩(shī),

你能學(xué)到的有:

網(wǎng)絡(luò)請(qǐng)求

可滾動(dòng)組件

狀態(tài)管理

常用組件

常用屬性

修改應(yīng)用名稱和圖標(biāo)

在Config.json添加權(quán)限等

用到的接口:

接口:

https://py.myie9.com/hidepoem/堅(jiān)果

請(qǐng)求方式:

Get

apipost請(qǐng)求測(cè)試

image-20220711081818157

接口:

https://py.myie9.com/xuxietest/汗滴禾下土

apipost請(qǐng)求測(cè)試:

image-20220711082102057

如何創(chuàng)建應(yīng)用在這里不做解釋。

首先預(yù)覽一下應(yīng)用

gif1

注意點(diǎn):

允許https需要添加下面的配置

"deviceConfig": {

"default": {

"network": {

"cleartextTraffic": true

}

}

},

使用網(wǎng)絡(luò)請(qǐng)求在config.json添加權(quán)限:

"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 = "堅(jiān)果的小跟班";

@State tibetanInput: string = "跟著堅(jiān)果學(xué)鴻蒙";

@State wholeContent: string = "";

@State wholeInput: string = "跟著堅(jiān)果學(xué)鴻蒙";

private scroller: Scroller = new Scroller()

?

?

?

onCancel() {

console.info('關(guān)閉')

}

?

?

?

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: '請(qǐng)輸入要生成的內(nèi)容', })

.fontSize(36)

.enterKeyType(EnterKeyType.Go)

.onChange((value) => {

this.tibetanInput = value;

?

})

.height(80)

.margin({

top: 40,

left: 16,

right: 16

})

?

Button("生成藏頭詩(shī)").backgroundColor(Color.Pink)

.onClick(() => {

this.TibetanRequest();

?

})

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

TextInput({ placeholder: '請(qǐng)輸入要生成的內(nèi)容', })

.fontSize(36)

.enterKeyType(EnterKeyType.Go)

.onChange((value) => {

this.wholeInput = value;

?

})

.height(80)

.margin({

?

left: 16,

right: 16

})

Button("生成整首詩(shī)").backgroundColor(Color.Green)

.onClick(() => {

this.wholePoemRequest();

})

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

}

.padding(10)

}

?

}

//藏頭詩(shī)接口

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);

}

?

}

}

?

);

}

?

//整首詩(shī)接口

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);

}

}

}

);

}

}

發(fā)起網(wǎng)絡(luò)請(qǐng)求

使用 @ohos.net.http 模塊發(fā)起網(wǎng)絡(luò)請(qǐng)求分為以下步驟:

引入http模塊

import

http

from

'@ohos.net.http'

;

創(chuàng)建一個(gè)httpRequest

let

httpRequest

=

http

.

createHttp

();

發(fā)起http請(qǐng)求

httpRequest 提供了兩種 request() 方法進(jìn)行網(wǎng)絡(luò)請(qǐng)求,分別是無 RequestOptions 參數(shù)的請(qǐng)求和有 RequestOptions 參數(shù)的請(qǐng)求。分別介紹如下:

RequestOptions 參數(shù)請(qǐng)求

  1. //藏頭詩(shī)接口
    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() 方法默認(rèn)采用 get 方式請(qǐng)求。

上述代碼,重點(diǎn)是通過調(diào)用HTTP的AI接口,來獲取生成接口返回的詩(shī)的內(nèi)容,并顯示在應(yīng)用界面上。

修改應(yīng)用描述信息

默認(rèn)的應(yīng)用描述信息,集中在config.json文件中。

image-20220711111409744

修改string.json內(nèi)容如下:

"srcLanguage": "ets",

"srcPath": "MainAbility",

"icon": "$media:icon", //應(yīng)用圖標(biāo)

"description": "$string:desc",

"label": "$string:title", //應(yīng)用名稱

"type": "page",

"visible": true,

"launchType": "standard"

這么有趣的應(yīng)用就這樣完成了,比起js開發(fā)方式,eTS是不是更為簡(jiǎn)單呢。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • HarmonyOS
    +關(guān)注

    關(guān)注

    79

    文章

    1958

    瀏覽量

    29916
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3614

    瀏覽量

    15989
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    第三屆OpenHarmony技術(shù)大會(huì)星光璀璨、致謝OpenHarmony社區(qū)貢獻(xiàn)者

    10月12日,在上海舉辦的第三屆OpenHarmony技術(shù)大會(huì)上,32家高校OpenHarmony技術(shù)俱樂部璀璨亮相,30家高校OpenHarmony開發(fā)者協(xié)會(huì)盛大啟幕。還分別致謝了年度星光TSG
    的頭像 發(fā)表于 10-21 14:10 ?108次閱讀

    基于ArkTS語(yǔ)言的OpenHarmony APP應(yīng)用開發(fā):HelloOpenharmony

    1、程序簡(jiǎn)介該程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)編寫的UI應(yīng)用類:HelloOpenHarmony。本案例是基于API9接口開發(fā)。本案例已在OpenHarmony凌蒙派-RK3568開發(fā)
    的頭像 發(fā)表于 09-15 08:09 ?227次閱讀
    基于ArkTS語(yǔ)言的<b class='flag-5'>OpenHarmony</b> APP應(yīng)用開發(fā):Hello<b class='flag-5'>Openharmony</b>

    鴻蒙OpenHarmony【創(chuàng)建工程并獲取源碼】

    在通過DevEco Device Tool創(chuàng)建OpenHarmony工程時(shí),可自動(dòng)下載相應(yīng)版本的OpenHarmony源碼。
    的頭像 發(fā)表于 04-19 21:40 ?310次閱讀
    鴻蒙<b class='flag-5'>OpenHarmony</b>【創(chuàng)建工程并獲取源碼】

    OpenHarmony南向能力征集令

    1、適配過程中缺少哪些接口能力或者南向能力,需要OpenHarmony去補(bǔ)齊的?例如內(nèi)核、編譯、器件適配、單板適配等; 2、對(duì)標(biāo)linux,需要OpenHarmony提供哪些能力?比如V4L2
    發(fā)表于 04-09 15:32

    OpenHarmony內(nèi)核編程實(shí)戰(zhàn)

    編程入門[Hello,OpenHarmony]在正式開始之前,對(duì)于剛接觸OpenHarmony的伙伴們,面對(duì)大篇幅的源碼可能無從下手,不知道怎么去編碼寫程序,下面用一個(gè)簡(jiǎn)單的例子帶伙伴們?nèi)腴T。▍任務(wù)
    的頭像 發(fā)表于 03-27 08:31 ?641次閱讀
    <b class='flag-5'>OpenHarmony</b>內(nèi)核編程實(shí)戰(zhàn)

    淺談兼容 OpenHarmony 的 Flutter

    OpenHarmony SIG 組織在 Gitee 開源了兼容 OpenHarmony 的 Flutter。該組織主要用于孵化 OpenHarmony 相關(guān)的開源生態(tài)項(xiàng)目。 ? ? ▲ 倉(cāng)庫(kù)地址
    的頭像 發(fā)表于 02-02 15:22 ?536次閱讀
    淺談兼容 <b class='flag-5'>OpenHarmony</b> 的 Flutter

    廈門大學(xué)OpenHarmony技術(shù)俱樂部正式揭牌成立

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個(gè)人的 OpenHarmony 12月29日下午,由OpenAtom OpenHarmony(簡(jiǎn)稱“OpenHarmony
    的頭像 發(fā)表于 01-04 21:15 ?485次閱讀
    廈門大學(xué)<b class='flag-5'>OpenHarmony</b>技術(shù)俱樂部正式揭牌成立

    OpenHarmony Meetup 2023南京站亮點(diǎn)搶先看

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個(gè)人的 OpenHarmony 原文標(biāo)題:OpenHarmony Meetup 2023南京站亮點(diǎn)搶先看 文章出處:【微信公眾號(hào):OpenAtom
    的頭像 發(fā)表于 12-25 21:10 ?524次閱讀
    <b class='flag-5'>OpenHarmony</b> Meetup 2023南京站亮點(diǎn)搶先看

    openharmony開發(fā)應(yīng)用

    隨著智能設(shè)備的普及和多樣化,開發(fā)者們對(duì)于更加靈活、高效的操作系統(tǒng)需求與日俱增。在這個(gè)背景下,華為推出了OpenHarmony,一個(gè)全場(chǎng)景智能終端操作系統(tǒng)和生態(tài)平臺(tái)。本文將詳細(xì)探討
    的頭像 發(fā)表于 12-19 09:42 ?590次閱讀

    九聯(lián)科技攜手惠州學(xué)院共建OpenHarmony創(chuàng)新實(shí)驗(yàn)室共筑OpenHarmony人才生態(tài)

    進(jìn)行OpenHarmony 創(chuàng)新實(shí)驗(yàn)室揭牌。 作為OpenHarmony核心共建單位之一和A類捐贈(zèng)人,九聯(lián)科技一直致力于推動(dòng)OpenHarmony技術(shù)的發(fā)展和應(yīng)用。為了更好地培養(yǎng)OpenHar
    的頭像 發(fā)表于 12-18 09:13 ?594次閱讀
    九聯(lián)科技攜手惠州學(xué)院共建<b class='flag-5'>OpenHarmony</b>創(chuàng)新實(shí)驗(yàn)室共筑<b class='flag-5'>OpenHarmony</b>人才生態(tài)

    OpenHarmony Meetup 2023北京站圓滿舉辦

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個(gè)人的 OpenHarmonyOpenHarmony正當(dāng)時(shí)”OpenHarmony Meetup 2023城市巡回活動(dòng)
    的頭像 發(fā)表于 11-28 21:10 ?574次閱讀
    <b class='flag-5'>OpenHarmony</b> Meetup 2023北京站圓滿舉辦

    OpenHarmony 4.0 Release版本發(fā)布

    編者按:潤(rùn)和軟件是OpenHarmony項(xiàng)目初始成員單位、A類捐贈(zèng)人、核心共建單位,控股子公司潤(rùn)開鴻聚焦OpenHarmony國(guó)產(chǎn)化數(shù)字技術(shù)底座,面向以垂直行業(yè)為代表的千行百業(yè)提供深度融合行業(yè)特征
    的頭像 發(fā)表于 11-18 08:02 ?456次閱讀
    <b class='flag-5'>OpenHarmony</b> 4.0 Release版本發(fā)布

    OpenHarmony技術(shù)大會(huì) | OpenHarmony技術(shù)俱樂部分論壇嘉賓金句

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個(gè)人的 OpenHarmony 原文標(biāo)題:OpenHarmony技術(shù)大會(huì) | OpenHarmony技術(shù)俱樂部分論壇嘉賓金
    的頭像 發(fā)表于 11-10 20:25 ?423次閱讀

    議程直擊 | 第二屆OpenHarmony技術(shù)大會(huì)——OpenHarmony技術(shù)俱樂部分論壇

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個(gè)人的 OpenHarmony 原文標(biāo)題:議程直擊 | 第二屆OpenHarmony技術(shù)大會(huì)——OpenHarmony技術(shù)
    的頭像 發(fā)表于 11-01 09:25 ?402次閱讀
    議程直擊 | 第二屆<b class='flag-5'>OpenHarmony</b>技術(shù)大會(huì)——<b class='flag-5'>OpenHarmony</b>技術(shù)俱樂部分論壇

    OpenHarmony Meetup 2023成都站圓滿舉辦

    點(diǎn)擊藍(lán)字 ╳ 關(guān)注我們 開源項(xiàng)目 OpenHarmony 是每個(gè)人的 OpenHarmonyOpenHarmony正當(dāng)時(shí)”OpenHarmony Meetup 2023城市巡回活動(dòng)
    的頭像 發(fā)表于 10-28 16:20 ?489次閱讀
    <b class='flag-5'>OpenHarmony</b> Meetup 2023成都站圓滿舉辦