Pokeapi 和获取 resource_uri
Pokeapi and getting resource_uri
所以我使用 AFNetworking 和 pokeapi 来获取 pokemon 列表。我可以让 tableview 加载,标题是口袋妖怪的名字。但我希望字幕是类型。所以我将基数设置为 #define pokeApiBaseURL [NSURL URLWithString: @"http://pokeapi.co/"]
这是我的请求的样子。我制作了一个数组,它为我提供了 url 的下半部分,我需要解析并获取类型和口袋妖怪的所有其他信息。但我似乎无法弄清楚如何使 baseURL 和 resource_uri 为我创建一个新的 url 来解析。
NSString *string = [NSString stringWithFormat:@"%@api/v1/pokedex/1/", pokeApiBaseURL];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.pokemonArray = [responseObject objectForKey:@"pokemon"];
self.pokemonDetailArray = [[responseObject objectForKey:@"pokemon"]valueForKey:@"resource_uri"];
// NSLog(@"\n%@",detailString);
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed: %@, %@", error, error.userInfo);
}];
[operation start];
这是我将它添加到单元格的地方
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdenifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdenifier forIndexPath:indexPath];
NSDictionary *pokemonDictionary = [self.pokemonArray objectAtIndex:indexPath.row];
NSDictionary *pokemonDetailDictionary = [self.pokemonDetailArray objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.textLabel sizeToFit];
cell.textLabel.text = [pokemonDictionary objectForKey:@"name"];
cell.detailTextLabel.text = @"PLACEHOLDER"; //WANT THE TYPE TO GO HERE!
return cell;
}
我想您可能正在寻找这种将资源与基础结合起来的方法:
+ (instancetype)URLWithString:(NSString *)URLString
relativeToURL:(NSURL *)baseURL
编辑
今天早上我有一些空闲时间,因为客户没有回复我,所以我做了这个:https://github.com/AlexTrott/PokeAPI:远非完美,这只是向您展示如何首先在本地存储数据加载,如果您有任何问题,请告诉我。
这里是获取您想要的 URL 和信息的方法(I used AFHTTPSessionManager as the method you use is about to be deprecated in AFN3) :
-(void)loadData {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://pokeapi.co/api/v1/pokedex/1/" parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
pokemonArray = [responseObject objectForKey:@"pokemon"];
pokemonDetailArray = [[responseObject objectForKey:@"pokemon"]valueForKey:@"resource_uri"];
[self loop];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)loop {
for (int i = 0; i < [pokemonArray count]; i++) {
[self loadMore:i];
}
}
-(void)loadMore:(int)pokemonID {
NSString *url = [NSString stringWithFormat:@"%@%@", pokeApiBaseURL, [pokemonDetailArray objectAtIndex:pokemonID]];
NSLog(@"URL = %@", url);
NSLog(@"Loop Number = %d", pokemonID);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"response = %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
我认为你需要重新考虑如何处理这个问题,我刚刚查看了 poke api,要执行你想要的操作,你必须进行 779 次调用,1 次是 pokedex 调用,然后另一个是778宝可梦
你可以通过删除 pokedex 请求,然后根据数据形成一个数组,将这个降低一个,但是你正在做 778 请求,这在我看来是疯狂的,我认为你需要解决你想如何显示数据,如果你最好不要第一次加载,或者你自己编译数据并在本地拥有它,因为 778 请求是疯狂的。
如果您确实必须执行那么多请求,请确保它只执行一次,并且是在他们第一次加载应用程序或其他内容时,然后存储在 CD 或领域或其他内容中。
有关将数据放入一个已排序的 NSMutableArray 的非常快速但肮脏的示例,请看一下这个,我不会这样做,我会对其进行批处理或在加载时存储在本地,但它应该让你更好地了解你想做什么。
#import "ViewController.h"
#import <AFNetworking/AFNetworking.h>
@interface ViewController () {
NSString *pokeApiBaseURL;
NSMutableArray *pokedex;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTable) name:@"orginalDone" object:nil];
pokeApiBaseURL = @"http://pokeapi.co/";
pokedex = [[NSMutableArray alloc] init];
for (int i = 0; i < 151; i++) {
[self loadData:i];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)loadData:(int)pokemonID {
NSString *url = [NSString stringWithFormat:@"http://pokeapi.co/api/v1/pokemon/%d/", pokemonID];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
[self addPokemon:responseObject];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)addPokemon:(id)pokemonObject {
NSMutableDictionary *pokemonData = [[NSMutableDictionary alloc] init];
[pokemonData setObject:pokemonObject[@"pkdx_id"] forKey:@"pkmnID"];
[pokemonData setObject:pokemonObject[@"name"] forKey:@"pokemonName"];
NSMutableArray *types = [[NSMutableArray alloc] init];
for (int i = 0; i < [pokemonObject[@"types"] count]; i++) {
[types addObject:pokemonObject[@"types"][i][@"name"]];
}
[pokemonData setObject:types forKey:@"types"];
[pokedex addObject:pokemonData];
NSLog(@"count = %lu", (unsigned long)[pokedex count]);
if ([pokedex count] == 150) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"orginalDone" object:nil];
}
}
-(void)updateTable {
NSLog(@"Unsorted = %@", pokedex);
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"pkmnID" ascending:YES];
[pokedex sortUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"Sorted = %@", pokedex);
}
@end
所以我使用 AFNetworking 和 pokeapi 来获取 pokemon 列表。我可以让 tableview 加载,标题是口袋妖怪的名字。但我希望字幕是类型。所以我将基数设置为 #define pokeApiBaseURL [NSURL URLWithString: @"http://pokeapi.co/"]
这是我的请求的样子。我制作了一个数组,它为我提供了 url 的下半部分,我需要解析并获取类型和口袋妖怪的所有其他信息。但我似乎无法弄清楚如何使 baseURL 和 resource_uri 为我创建一个新的 url 来解析。
NSString *string = [NSString stringWithFormat:@"%@api/v1/pokedex/1/", pokeApiBaseURL];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.pokemonArray = [responseObject objectForKey:@"pokemon"];
self.pokemonDetailArray = [[responseObject objectForKey:@"pokemon"]valueForKey:@"resource_uri"];
// NSLog(@"\n%@",detailString);
[self.tableView reloadData];
[self.refreshControl endRefreshing];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed: %@, %@", error, error.userInfo);
}];
[operation start];
这是我将它添加到单元格的地方
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdenifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdenifier forIndexPath:indexPath];
NSDictionary *pokemonDictionary = [self.pokemonArray objectAtIndex:indexPath.row];
NSDictionary *pokemonDetailDictionary = [self.pokemonDetailArray objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.textLabel sizeToFit];
cell.textLabel.text = [pokemonDictionary objectForKey:@"name"];
cell.detailTextLabel.text = @"PLACEHOLDER"; //WANT THE TYPE TO GO HERE!
return cell;
}
我想您可能正在寻找这种将资源与基础结合起来的方法:
+ (instancetype)URLWithString:(NSString *)URLString
relativeToURL:(NSURL *)baseURL
编辑
今天早上我有一些空闲时间,因为客户没有回复我,所以我做了这个:https://github.com/AlexTrott/PokeAPI:远非完美,这只是向您展示如何首先在本地存储数据加载,如果您有任何问题,请告诉我。
这里是获取您想要的 URL 和信息的方法(I used AFHTTPSessionManager as the method you use is about to be deprecated in AFN3) :
-(void)loadData {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://pokeapi.co/api/v1/pokedex/1/" parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
pokemonArray = [responseObject objectForKey:@"pokemon"];
pokemonDetailArray = [[responseObject objectForKey:@"pokemon"]valueForKey:@"resource_uri"];
[self loop];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)loop {
for (int i = 0; i < [pokemonArray count]; i++) {
[self loadMore:i];
}
}
-(void)loadMore:(int)pokemonID {
NSString *url = [NSString stringWithFormat:@"%@%@", pokeApiBaseURL, [pokemonDetailArray objectAtIndex:pokemonID]];
NSLog(@"URL = %@", url);
NSLog(@"Loop Number = %d", pokemonID);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"response = %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
我认为你需要重新考虑如何处理这个问题,我刚刚查看了 poke api,要执行你想要的操作,你必须进行 779 次调用,1 次是 pokedex 调用,然后另一个是778宝可梦
你可以通过删除 pokedex 请求,然后根据数据形成一个数组,将这个降低一个,但是你正在做 778 请求,这在我看来是疯狂的,我认为你需要解决你想如何显示数据,如果你最好不要第一次加载,或者你自己编译数据并在本地拥有它,因为 778 请求是疯狂的。
如果您确实必须执行那么多请求,请确保它只执行一次,并且是在他们第一次加载应用程序或其他内容时,然后存储在 CD 或领域或其他内容中。
有关将数据放入一个已排序的 NSMutableArray 的非常快速但肮脏的示例,请看一下这个,我不会这样做,我会对其进行批处理或在加载时存储在本地,但它应该让你更好地了解你想做什么。
#import "ViewController.h"
#import <AFNetworking/AFNetworking.h>
@interface ViewController () {
NSString *pokeApiBaseURL;
NSMutableArray *pokedex;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTable) name:@"orginalDone" object:nil];
pokeApiBaseURL = @"http://pokeapi.co/";
pokedex = [[NSMutableArray alloc] init];
for (int i = 0; i < 151; i++) {
[self loadData:i];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)loadData:(int)pokemonID {
NSString *url = [NSString stringWithFormat:@"http://pokeapi.co/api/v1/pokemon/%d/", pokemonID];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil success:^(NSURLSessionTask *task, id responseObject) {
[self addPokemon:responseObject];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)addPokemon:(id)pokemonObject {
NSMutableDictionary *pokemonData = [[NSMutableDictionary alloc] init];
[pokemonData setObject:pokemonObject[@"pkdx_id"] forKey:@"pkmnID"];
[pokemonData setObject:pokemonObject[@"name"] forKey:@"pokemonName"];
NSMutableArray *types = [[NSMutableArray alloc] init];
for (int i = 0; i < [pokemonObject[@"types"] count]; i++) {
[types addObject:pokemonObject[@"types"][i][@"name"]];
}
[pokemonData setObject:types forKey:@"types"];
[pokedex addObject:pokemonData];
NSLog(@"count = %lu", (unsigned long)[pokedex count]);
if ([pokedex count] == 150) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"orginalDone" object:nil];
}
}
-(void)updateTable {
NSLog(@"Unsorted = %@", pokedex);
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"pkmnID" ascending:YES];
[pokedex sortUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"Sorted = %@", pokedex);
}
@end