#跟着坚果学鸿蒙#藏头诗应用
·
藏头诗应用
创建项目
后端接口
文档地址:https://www.alapi.cn/api/28/api_document
接口地址
https://v3.alapi.cn/api/ai/poem
请求参数
序号 | 参数名 | 类型 | 是否必填 | 说明 |
---|---|---|---|---|
1 | token | 是 | 接口调用token,需要在 token管理 中创建 | |
2 | keyword | string | 是 | 藏字内容,2-8个字 |
3 | num | string | 否 | 诗句格式,5五言诗[默认]、7七言诗 |
4 | type | string | 否 | 藏头位置,1藏头[默认]、2藏尾、3藏中、4递增、5递减 |
5 | rhyme | string | 否 | 押韵类型,1双句一压[默认]、2双句押韵、3一三四押 |
网络请求的流程
场景介绍
应用通过HTTP发起一个数据请求,支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。
1.添加网络权限
"requestPermissions": [ { "name": "ohos.permission.INTERNET" } ],
2.从@kit.NetworkKit中导入http命名空间
import { http } from '@kit.NetworkKit';
3.调用createHttp()方法,创建一个HttpRequest对象
//3.调用createHttp()方法,创建一个HttpRequest对象。 let httpRequest = http.createHttp()
4.调用该对象的on()方法,订阅http响应头事件(可选)
此接口会比request请求先返回。可以根据业务需要订阅此消息。
//4.调用该对象的on()方法,订阅http响应头事件 httpRequest.on('headersReceive', (header) => { console.info('header: ' + JSON.stringify(header)); });
5.调用该对象的request()方法,传入http请求的url地址和可选参数,发起网络请求。
httpRequest.request("https://v3.alapi.cn/api/ai/poem", { method: http.RequestMethod.GET, extraData: { token: "你的token", keyword: "鸿蒙生态千帆起", } }, (err: BusinessError, data: http.HttpResponse) => { })
6.按照实际业务需要,解析返回结果
(err: BusinessError, data: http.HttpResponse) => { //6.按照实际业务需要,解析返回结果 if (err) { promptAction.showToast({ message: "请求有问题" }) console.error('error:' + JSON.stringify(err)); } else { console.info("后端返回的值", JSON.stringify(data.result)) } }
7.调用该对象的off()方法,取消订阅http响应头事件。
if (err) { promptAction.showToast({ message: "请求有问题" }) console.error('error:' + JSON.stringify(err)); } else { console.info("后端返回的值", JSON.stringify(data.result)) // 7.取消订阅HTTP响应头事件 httpRequest.off('headersReceive'); } }
8.当该请求使用完毕时,调用destroy()方法主动销毁。
if (err) { promptAction.showToast({ message: "请求有问题" }) console.error('error:' + JSON.stringify(err)); // 8.当该请求使用完毕时,调用destroy方法主动销毁 httpRequest.destroy(); } else { console.info("后端返回的值", JSON.stringify(data.result)) // 7.取消订阅HTTP响应头事件 httpRequest.off('headersReceive'); // 8.当该请求使用完毕时,调用destroy方法主动销毁 httpRequest.destroy(); } }
9.网络请求完整代码
getData() { //3.调用createHttp()方法,创建一个HttpRequest对象。 let httpRequest = http.createHttp() //4.调用该对象的on()方法,订阅http响应头事件 httpRequest.on('headersReceive', (header) => { console.info('header: ' + JSON.stringify(header)); }); //5.调用该对象的request()方法,传入http请求的url地址和可选参数,发起网络请求。 httpRequest.request("https://v3.alapi.cn/api/ai/poem", { method: http.RequestMethod.GET, extraData: { token: "你的token", keyword: "鸿蒙生态千帆起", } }, (err: BusinessError, data: http.HttpResponse) => { //6.按照实际业务需要,解析返回结果 if (err) { promptAction.showToast({ message: "请求有问题" }) console.error('error:' + JSON.stringify(err)); // 8.当该请求使用完毕时,调用destroy方法主动销毁 httpRequest.destroy(); } else { console.info("后端返回的值", JSON.stringify(data.result)) // 7.取消订阅HTTP响应头事件 httpRequest.off('headersReceive'); // 8.当该请求使用完毕时,调用destroy方法主动销毁 httpRequest.destroy(); } }) }
返回数据的处理
{ "request_id": "781477263893757952", "success": true, "message": "success", "code": 200, "data": [ { "riddle": "灶王爷扫院子", "answer": "多管闲事" } ], "time": 1747278698, "usage": 0 }
相关链接
完毕,
更多推荐
所有评论(0)