qt调用simsimi api实现小黄鸡

2019-02-25 15:28:21 浏览数 (1)

项目地址:https://github.com/racaljk/xiaojianji

好吧我把它命名为小贱鸡.,下面说一说他的实现。

由于官方的simsimi api需要收费,免费试用版有各种限制,所以这里使用的www.xiaohuangji.com 然后post实现,ui界面有一个lineEdit消息输入框,一个TextEdit来显示,一个pushButton发送消息

xiaojainji.cpp

代码语言:javascript复制
//使用www.xiaohuangji.com进行post
void xiaojianji::on_pushButton_clicked()
{
    QString str = ui->lineEdit->text();  
    ui->textEdit->append("我 :"   str "n");   //在textedit显示消息
    ui->lineEdit->clear();    //发送后清除linedit的文字
    QTextCodec *utf8 = QTextCodec::codecForName("utf-8");    
    QByteArray msg = "para="   utf8->fromUnicode(str).toPercentEncoding();   //最后post msg
    QNetworkRequest request;      
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");  //header
    request.setHeader(QNetworkRequest::ContentLengthHeader,msg.length());  
    request.setUrl(QUrl("http://www.xiaohuangji.com/ajax.php"));
    QNetworkReply *reply  = manager->post(request,msg);  //进行post 
}

void xiaojianji::replyFinished(QNetworkReply *reply)
{
  if(reply->error() == QNetworkReply::NoError)
  {
      QString out(reply->readAll());  //接受post后的数据
      ui->textEdit->append("小贱鸡 :"   out   "n");  //在textedit显示
  }else{
      ui->textEdit->append("小贱鸡 : 好像出了点问题...n");
  }
}

就这么简单,如果用php会更简单...

0 人点赞