//解析反馈结果
void Widget::replyFinished(QNetworkReply *reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
?
//读取所有数据
QByteArray replyData = reply->readAll();
?
qDebug()<< "状态码:" <<statusCode;
qDebug()<< "反馈的数据:" <<QString(replyData);
?
//判断状态码
if (200 != statusCode)
{
return ;
}
?
/*
反馈的数据: "{"couplets":{"center":"万象皆春","first":"龙年龙舞千秋业","second":"蛇岁蛇腾万里春"},"log_id":1484407725829006888}"
?
上联: "龙年龙舞千秋业"
下联: "蛇岁蛇腾万里春"
横批: "万象皆春"
?
上联: "虎跃龙腾千里虎"
下联: "龙腾虎跃万家龙"
横批: "虎跃龙腾"
?
*/
if (function_select==1) //春联生成
{
//解析数据
QJsonParseError json_error;
QJsonDocument document = QJsonDocument::fromJson(replyData, &json_error);
if (json_error.error == QJsonParseError::NoError)
{
//判断是否是对象,然后开始解析数据
if (document.isObject())
{
QJsonObject obj = document.object();
//解析错误代码
if (obj.contains( "error_code" ))
{
int error_code=obj.take( "error_code" ).toInt();
switch (error_code)
{
case 0: //成功
break ;
case 110:
case 111:
QMessageBox::information( this , "提示" , "正在更新密匙.确定后重试." ,
QMessageBox::Ok,QMessageBox::Ok);
?
//更新密匙
QTimer::singleShot(200, this , SLOT(updateCaption()));
return ;
?
default :
if (obj.contains( "error_msg" ))
{
QString text=obj.take( "error_msg" ).toString();
QMessageBox::information( this , "提示" ,text,
QMessageBox::Ok,QMessageBox::Ok);
return ;
}
}
}
else if (obj.contains( "couplets" ))
{
QJsonObject obj2=obj.take( "couplets" ).toObject();
?
QString center; //横批
QString first; //上联-第一
QString second; //下联第二
?
if (obj2.contains( "center" ))
{
center=obj2.take( "center" ).toString();
}
if (obj2.contains( "first" ))
{
first=obj2.take( "first" ).toString();
}
if (obj2.contains( "second" ))
{
second=obj2.take( "second" ).toString();
}
?
ui->label_0->setText(center);
?
QString first_1= "\n" ;
for ( int i=0;i<first.size();i++)
{
first_1+=first.at(i);
first_1+= "\n" ;
}
?
QString second_1= "\n" ;
for ( int i=0;i<second.size();i++)
{
second_1+=second.at(i);
second_1+= "\n" ;
}
?
ui->label_1->setText(first_1);
ui->label_2->setText(second_1);
?
qDebug()<< "上联:" <<first;
qDebug()<< "下联:" <<second;
qDebug()<< "横批:" <<center;
}
}
}
}
//更新token
else if (function_select==4)
{
//解析数据
QJsonParseError json_error;
QJsonDocument document = QJsonDocument::fromJson(replyData, &json_error);
if (json_error.error == QJsonParseError::NoError)
{
//判断是否是对象,然后开始解析数据
if (document.isObject())
{
QJsonObject obj = document.object();
//解析token
if (obj.contains( "access_token" ))
{
QString data_token=obj.take( "access_token" ).toString();
Token=data_token.toUtf8();
qDebug()<< "更新的Token:" <<data_token;
//保存到文件
SaveDataToFile(Token);
QMessageBox::information( this , "提示" , "密匙更新成功." ,
QMessageBox::Ok,QMessageBox::Ok);
}
}
}
return ;
}
}
|