//
// RootViewController.m
// JsonPetser33
//
// Created by ME on 9/26/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "RootViewController.h"
//
#import "SBJson.h"
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
//
@implementation RootViewController
//
@synthesize mNetworkQueue;
@synthesize mArrData;
//
- (void)viewDidLoad
{
[super viewDidLoad];
//adding right button
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Load"
style:UIBarButtonItemStyleDone
target:self
action:@selector(loadData)];
}
-(void) loadData{
//Stop anything already in the queue before removing it
[[self mNetworkQueue] cancelAllOperations];
//Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
[self setMNetworkQueue:[ASINetworkQueue queue]];
[[self mNetworkQueue] setDelegate:self];
[[self mNetworkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self mNetworkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self mNetworkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];
//create url request using ASIHTTPRequest
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL
URLWithString:@"http://mikan-box.x10.bz/testing/json_test.php"]];
[[self mNetworkQueue] addOperation:request];
[[self mNetworkQueue] go];
}
//ASIHTTPRequest protocol?
- (void) requestFinished: (ASIHTTPRequest *)request{
//You could release the queue here if you wanted
if ([[self mNetworkQueue] requestsCount] == 0) {
//Since this is a retained property, setting it to nil will release it
//This is the safest way to handle releasing things - most of the time you only ever need to release in your accessors
//And if you an Objective-C 2.0 property for the queue (as in this example) the accessor is generated for you
[self setMNetworkQueue:nil];
}
//... Handle success
//parsing the data
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *dicTemp = [jsonParser objectWithData:[request responseData]];//parse json
mArrData = [dicTemp objectForKey:@"markers"];
//do something like loading data in table for now NSLog data
NSLog(@"markers: %@",mArrData); //here the app freezes can't click button etc for a few seconds//
NSLog(@"count %d",[mArrData count]); // how can i enhance it?
[jsonParser release];
NSLog(@"Request finished");
}
- (void) requestFailed:(ASIHTTPRequest *)request{
//You could release the queue here if you wanted
if ([[self mNetworkQueue] requestsCount] == 0) {
[self setMNetworkQueue:nil];
}
//... Handle failure
NSLog(@"Request failed: %@",[[request error] localizedDescription]);
}
-(void) queueFinished:(ASIHTTPRequest *) queue{
//You could release the queue here if you wanted
if ([[self mNetworkQueue] requestsCount] == 0) {
[self setMNetworkQueue:nil];
}
NSLog(@"Queue finished");
}
//
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc
{
[mNetworkQueue reset];
[mNetworkQueue release];
[super dealloc];
}
@end
- 我是objective-c和ios的新手。我使用ASIHTTPRequest,因为它已经有一个httphandler ..
- 问题是当我从给定的url(它的json)发出http请求时,它在解析和显示“NSLog(@”markers:%@“,mArrData)”期间冻结。有没有办法改进这段代码?
- 我想改进此代码,就像在另一个线程中执行它一样,就像为 url 请求完成的 ASINetworkQueue
- 我听说过 gcd(Grand Central Dispatch),但不知道它是否有用,也不知道它是否有用。
- 提前谢谢