Pokeapi和获取resource_uri

时间:2015-11-30 03:07:25

标签: arrays json dictionary afnetworking

所以我使用AFNetworking和pokeapi获取口袋妖怪列表。我可以让tableview加载标题是口袋妖怪的名字。但我希望副标题是类型。所以我将基数设置为#define pokeApiBaseURL [NSURL URLWithString: @"http://pokeapi.co/"]

以下是我的要求。我创建了一个数组,它给了我需要解析的下一半网址,并获取该类型以及所有其他的口袋妖怪信息。但我似乎无法弄清楚如何使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;

}

2 个答案:

答案 0 :(得分:1)

修改

今天早上我有一些空闲时间,因为客户没有回复我,并且掀起了这个:https://github.com/AlexTrott/PokeAPI:远非完美,这只是向您展示如何在首次加载时本地存储数据,如果您有任何问题,请告诉我。

以下是如何获取您想要的网址和信息 (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

答案 1 :(得分:0)

我认为您可能正在寻找这种方法将资源与基础相结合:

var upload = require('multer')({ dest: 'uploads/' });
var admZip = require('adm-zip');


app.post('/upload-here', upload.single('file'), function (req, res, next) {
    var zip = new admZip(req.file.path);

    zip.extractAllTo("C://TestFolder//TestZip", true);
});

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/#//apple_ref/occ/clm/NSURL/URLWithString:relativeToURL