//每当编辑完问题后
//1. 显示自己的问题 messageType=1
//2. 调用API,返回结果
-( void )KeyBordView:(KeyBordVIew *)keyBoardView textFiledReturn:(UITextField *)textFiled
{
//显示自己的问题
ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init];
ChartMessage *chartMessage=[[ChartMessage alloc]init];
[email protected] "icon01.png" ;
chartMessage.messageType=1;
chartMessage.content=textFiled.text;
cellFrame.chartMessage=chartMessage;
[self.cellFrames addObject:cellFrame];
[self.tableView reloadData];
//滚动到当前行
[self tableViewScrollCurrentIndexPath];
//利用用户问题, 查询结果
//API请求格式。 具体格式见图灵官网
//6c2cfaf7a7f088e843b550b0c5b89c26 替换成你申请的key即可
NSString* urlString = [NSString stringWithFormat:@ "http://www.tuling123.com/openapi/api?key=6c2cfaf7a7f088e843b550b0c5b89c26&&info=%@" , textFiled.text];
//NSUTF8StringEncoding编码。 避免中文错误
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//调用API
NSURL *url = [NSURL URLWithString:urlString];
testRequest = [ASIHTTPRequest requestWithURL:url];
[testRequest setDelegate:self];
[testRequest startAsynchronous];
[email protected] "" ;
myTextField = textFiled;
}
#pragma mark - 返回机器人回答
//调用API完毕, 返回图灵回答结果
//1. 收起键盘
//2. 显示回答内容
- ( void )requestFinished:(ASIHTTPRequest *)request
{
//收起键盘
[myTextField resignFirstResponder];
// 当以文本形式读取返回内容时用这个方法
// 解析返回的json数据
NSString *responseString = [request responseString];
self.testDic = [responseString objectFromJSONString];
self.testArr = [testDic objectForKey:@ "text" ];
//显示回答内容
ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init];
ChartMessage *chartMessage=[[ChartMessage alloc]init];
[email protected] "icon02.png" ;
chartMessage.messageType=0;
chartMessage.content=[NSString stringWithFormat:@ "%@" , self.testArr];
cellFrame.chartMessage=chartMessage;
[self.cellFrames addObject:cellFrame];
[self.tableView reloadData];
//滚动到当前行
[self tableViewScrollCurrentIndexPath];
}
// API请求失败
- ( void )requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@ "error --- %@" ,error);
UIAlertView *alert_ = [[UIAlertView alloc]initWithTitle:@ "提示"
message:@ "无网络可用,请检查网络状态"
delegate:self cancelButtonTitle:@ "知道了"
otherButtonTitles: nil];
[alert_ show];
}
|