如果我想从 iOS 应用程序中使用 REST 服务,那么最成熟的库是什么?我最好的选择是什么?
问问题
9991 次
5 回答
7
You can use RestKit.
Or you can use NSJSONSerialization
which is already in the foundation framework.
Here's an exemple from a project that i made. It fetch an array of drinks from a json webservice:
NSString *urlString= [NSString stringWithFormat:@"my json webservice URL"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *jsonResultSet = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if ([jsonResultSet count] !=0){
NSArray* drinks = [jsonResultSet objectForKey:@"drinks"];
for (NSDictionary* drinkDictionary in drinks) {
Drink* drink = [[Drink alloc] initWithDictionary:drinkDictionary];
[[DrinkList getInstance]addDrinksWithObject:drink];
}
}
于 2012-10-20T22:50:04.523 回答
1
我推荐AFNetworking。以下是取自其Github 页面的示例代码(那里有更多示例):
NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];
[operation start];
其他替代方案是:
其中,我认为 AFNetworking 和 RestKit 都是最受欢迎的。我个人已经广泛使用了 AFNetworking,因此我推荐它。
于 2012-10-21T03:07:15.943 回答
1
不太成熟但重量很轻:JNRestClient
于 2014-02-07T17:38:38.600 回答
1
还有一个名为 Alamofire ( https://github.com/Alamofire/Alamofire ) 的新的 swift rest 库,看起来非常不错,它在 github 上已经有 6000 多个 star,它是由 Mattt Thompson 创建的,这意味着你可以无忧无虑地去做。
于 2015-02-16T13:28:34.483 回答