为什么我调用有道 API 非常慢

2017-03-04 21:07:04 +08:00
 henryshen233
买的同样调用有道 API 的字典 Dictionary for iOS 就非常快,我是在 macOS App 开发上调用,却非常慢,常常要五秒以上才会返回结果,我用的是 NSJSONSerialization 来解析 JSON ,请问是否有什么问题,谢谢大家了
3220 次点击
所在节点    iDev
1 条回复
henryshen233
2017-03-04 21:34:15 +08:00
NSMutableString *emptyStr = [[NSMutableString alloc] initWithString:@"\n "];
NSString *baseStr = @"http://fanyi.youdao.com/openapi.do?keyfrom=LoveDict&key=1783061912&type=data&doctype=json&version=1.1&q=";
NSString *appenedStr = [baseStr stringByAppendingString:_searchText.stringValue];
NSURL *url = [NSURL URLWithString:appenedStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"GET";
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
self.dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *translationArray = [self.dic objectForKey:@"translation"];
NSString *translation = [translationArray objectAtIndex:0];
NSDictionary *basicDic = [self.dic objectForKey:@"basic"];
NSString *ukphonetic = [basicDic objectForKey:@"uk-phonetic"];
NSString *usphonetic = [basicDic objectForKey:@"us-phonetic"];
NSArray *explainsArray = [basicDic objectForKey:@"explains"];
for (NSString *eachExplain in explainsArray){
[emptyStr appendString:eachExplain];
[emptyStr appendString:@"\n "];
}
_resultText.stringValue = [NSString stringWithFormat:@" 翻译: %@\n 英式发音: [%@]\n 美式发音: [%@]\n 释义: %@",
translation, ukphonetic, usphonetic, emptyStr];
}] resume];

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/344959

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX