[注意]转载时请注明出处博客园-吃唐僧肉的小悟空http://www.cnblogs.com/hukezhu/
首先感谢 枯龙吟 网易博客提供了【成语词典】【新华字典】【汉语词典】接口以供使用.
无意中在网上发现了一个关于汉语字典方面的接口 ,于是自己就测试了一下,发现可以使用,非常感谢那位大神!!!
附上demo截图:
主要用到了数据解析的方式,服务器返回的是json数据,直接使用ios提供的类进行解析,这个小demo很简单,直接附上源码:(代码中,我封装了一个方法,传入服务器的地址和请求的数据,回到主线程更新UI)
1//2//ViewController.m3//新华字典4//5//Created by hukezhu on 15/7/7.6//Copyright (c) 2015年 hukezhu. All rights reserved.7//89#import"ViewController.h"10#import<AFNetworking.h>1112@interfaceViewController ()13@PRoperty (weak, nonatomic) IBOutlet UITextField *Word;14@property (weak, nonatomic) IBOutlet UITextField *chengyu;15@property (weak, nonatomic) IBOutlet UITextView *textView;16@property (weak, nonatomic) IBOutlet UITextField *ciyu;1718- (IBAction)wordClick:(UIButton *)sender;19- (IBAction)chengyuClick:(UIButton *)sender;20- (IBAction)ciyuClick:(UIButton *)sender;2122@end2324@implementationViewController2526272829- (void)viewDidLoad {30[super viewDidLoad];3132}333435- (void)getDictionary :(NSString *)strUrl word:(NSString *)word{3637NSString *stringUrl =strUrl;38AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];39manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];4041[manager GET:stringUrl parameters:@{@"word":word} success:^(AFHTTPRequestOperation *operation,idjson) {4243NSData *data = [NSJSONSerialization dataWithJSONObject:json options:0error:NULL];444546NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0error:NULL];47484950NSString *rootKey =dict.keyEnumerator.nextObject;51NSArray *array =dict[rootKey];52535455NSMutableString *stringM = [NSMutableStringstring];56for(NSDictionary *dict1inarray) {5758for(NSString *keyindict1) {59if([key isEqualToString:@"text"]) {60NSString *object=[dict1 objectForKey:key];61//NSLog(@"%@",object);62[stringM appendFormat:@"%@\n",object];63}6465}66}6768self.textView.text =stringM;69} failure:^(AFHTTPRequestOperation *operation, NSError *error) {70NSLog(@"error:%@",error);71}];727374}7576- (IBAction)wordClick:(UIButton *)sender {7778//NSLog(@"点击了查找按钮");79if(self.word.text) {80[self getDictionary:@"http://brisk.eu.org/api/xhzd.php"word:self.word.text];81}8283[self.view endEditing:YES];848586}8788- (IBAction)chengyuClick:(UIButton *)sender {8990if(self.chengyu.text) {91[self getDictionary:@"http://brisk.eu.org/api/cycd.php"word:self.chengyu.text];92}9394[self.view endEditing:YES];95}9697- (IBAction)ciyuClick:(UIButton *)sender {9899if(self.ciyu.text) {100[self getDictionary:@"http://brisk.eu.org/api/hycd.php"word:self.ciyu.text];101}102103104//这个是周公解梦的,可是api不能使用了105//if (self.ciyu.text) {106//[self getDictionary:@"http://brisk.eu.org/api/dream.php" word:self.ciyu.text];107//}108109[self.view endEditing:YES];110}111@end