V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
henryshen233
V2EX  ›  iDev

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

  •  
  •   henryshen233 · 2017-03-04 21:07:04 +08:00 · 3220 次点击
    这是一个创建于 2822 天前的主题,其中的信息可能已经有所发展或是发生改变。
    买的同样调用有道 API 的字典 Dictionary for iOS 就非常快,我是在 macOS App 开发上调用,却非常慢,常常要五秒以上才会返回结果,我用的是 NSJSONSerialization 来解析 JSON ,请问是否有什么问题,谢谢大家了
    1 条回复    2017-03-04 21:34:15 +08:00
    henryshen233
        1
    henryshen233  
    OP
       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];
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2876 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:55 · PVG 22:55 · LAX 06:55 · JFK 09:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.