使用 AFNetworking 显示来自 URL 的 JSON 数据
Displaying JSON Data from URL using AFNetworking
我想显示来自 URL 的 json 数据。
Everything Works 数据数组只能在加载部分访问。当我用它来计数时,它给出 NULL 但在内部确实加载它有效。什么问题。
.h 文件
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSDictionary *dictArray;
NSString *title;
}
.m 文件
#import "ViewController.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperation.h"
@interface ViewController ()
{
NSArray *data;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"sampleUrl"]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;
data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" message:[NSString stringWithFormat:@"%@", error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count]; // doesn;t work
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
dictArray = [data objectAtIndex:indexPath.row];
cell.textLabel.text = [dictArray objectForKey:@"title"];
NSLog(@"%@",data); // Doesn;t work displays NULL
return cell;}
@end
在 AFNetworking 管理器的成功块中,重新加载您的 tableView。 Table 现在正在加载 Web 服务得到响应之前。
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;
data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
[yourTableView reloadData];
}
我想显示来自 URL 的 json 数据。 Everything Works 数据数组只能在加载部分访问。当我用它来计数时,它给出 NULL 但在内部确实加载它有效。什么问题。
.h 文件
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSDictionary *dictArray;
NSString *title;
}
.m 文件
#import "ViewController.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperation.h"
@interface ViewController ()
{
NSArray *data;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"sampleUrl"]
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;
data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" message:[NSString stringWithFormat:@"%@", error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count]; // doesn;t work
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
dictArray = [data objectAtIndex:indexPath.row];
cell.textLabel.text = [dictArray objectForKey:@"title"];
NSLog(@"%@",data); // Doesn;t work displays NULL
return cell;}
@end
在 AFNetworking 管理器的成功块中,重新加载您的 tableView。 Table 现在正在加载 Web 服务得到响应之前。
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *weather = (NSDictionary *)responseObject;
data = [weather objectForKey:@"slider"];
NSLog(@"%@",data); // Works Fine
[yourTableView reloadData];
}