网络请求实现AI聊天,应用可以通过HTTP发起一个数据请求,再解析返回的数据来实现简单ai聊天功能

应用可以通过HTTP发起一个数据请求,再解析返回的数据来实现简单ai聊天功能。

HarmonyOS
2024-05-28 20:59:56
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
hujianwu

使用的核心API

request

核心代码:

import http from '@ohos.net.http'; 
import { BusinessError } from '@ohos.base'; 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello'; 
  @State req: string = "http://ajax-api.itheima.net/api/robot?spoken='你好'"; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
        Button('你是谁').onClick(() => { 
          this.message = "你的名字是什么"; 
          this.req = "http://ajax-api.itheima.net/api/robot?spoken='你的名字是什么'" 
        }) 
        Button('你在干嘛').onClick(() => { 
          this.message = "你在干嘛"; 
          this.req = "http://ajax-api.itheima.net/api/robot?spoken='你在干嘛'" 
        }) 
        Button('吃饭了吗').onClick(() => { 
          this.message = '你吃饭了吗'; 
          this.req = "http://ajax-api.itheima.net/api/robot?spoken='吃饭了吗'" 
        }) 
        Button('你的主人').onClick(() => { 
          this.message = '你的主人是谁'; 
          this.req = "http://ajax-api.itheima.net/api/robot?spoken=你的主人是谁" 
 
        }) 
        Button('send') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            httpRequest.request(// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定 
              this.req, 
              { 
                method: http.RequestMethod.GET, // 可选,默认为http.RequestMethod.GET 
                // 当使用POST请求时此字段用于传递内容 
                // extraData: '吃饭了吗', 
                expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型 
                usingCache: false, // 可选,默认为true 
                priority: 1, // 可选,默认为1 
                // 开发者根据自身业务需要添加header字段 
                header: new Header('application/json'), 
                readTimeout: 60000, // 可选,默认为60000ms 
                connectTimeout: 60000, // 可选,默认为60000ms 
                usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定 
                usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性 
                caPath: "", // 可选,默认使用系统预设CA证书,自API 10开始支持该属性 
              }, 
              (err: BusinessError, data: http.HttpResponse) => { 
                if (!err) { 
                  let data1 = JSON.stringify(data.result) 
                  console.log('88899' + data1) 
                  AlertDialog.show({ message: data1 }) 
                  // data.result为HTTP响应内容,可根据业务需要进行解析 
                  console.info('Result88899:' + JSON.stringify(data.result)); 
                  console.info('code:' + JSON.stringify(data.responseCode)); 
                  console.info('type:' + JSON.stringify(data.resultType)); 
                  // data.header为HTTP响应头,可根据业务需要进行解析 
                  console.info('header88899:' + JSON.stringify(data.header)); 
                  console.info('cookies:' + JSON.stringify(data.cookies)); // 自API version 8开始支持cookie 
                  // 取消订阅HTTP响应头事件 
                  // httpRequest.off('headersReceive'); 
                  // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 
                  // httpRequest.destroy(); 
                } else { 
                  console.info('error:' + JSON.stringify(err)); 
                  // 取消订阅HTTP响应头事件 
                  // httpRequest.off('headersReceive'); 
                  // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 
                  // httpRequest.destroy(); 
                } 
              }); 
            // this.message='你好' 
            // AlertDialog.show({message:'你好'}) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
 
// 每一个httpRequest对应一个HTTP请求任务,不可复用 
let httpRequest = http.createHttp(); 
// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息 
// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+ 
httpRequest.on('headersReceive', (header: Object) => { 
  console.info('header: ' + JSON.stringify(header)); 
}); 
 
class ExtraData { 
  public spoken: string; 
 
  constructor(data: string) { 
    this.spoken = data; 
  } 
} 
 
class Header { 
  public contentType: string; 
 
  constructor(contentType: string) { 
    this.contentType = contentType; 
  } 
}

实现效果

发送网络请求,通过请求内容返回不同的data实现聊天功能,当前仅支持将返回内容弹窗展示。

分享
微博
QQ
微信
回复
2024-05-29 22:02:23
相关问题
使用http模块实现数据请求
204浏览 • 1回复 待解决
webview如何实现网络请求拦截功能
584浏览 • 1回复 待解决
http request 请求不到接口数据
3239浏览 • 1回复 待解决
websocket和http数据请求示例
217浏览 • 1回复 待解决
使用http请求网络地址
272浏览 • 1回复 待解决
网络请求使用gzip压缩数据
374浏览 • 1回复 待解决
鸿蒙JS开发HTTP请求如何解析
1230浏览 • 0回复 待解决
鸿蒙应用开发请求不到数据
6298浏览 • 2回复 待解决
SQL插入一个数据时,如何获取ID?
1670浏览 • 2回复 待解决
真机运行http请求没有返回
2947浏览 • 1回复 待解决
rcp模块能力发起post请求
236浏览 • 1回复 待解决
应用http请求响应2300023
870浏览 • 1回复 待解决
http 请求直报 2300058
335浏览 • 0回复 待解决