1
//
// 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的..
- 的問題是,當我從做HTTP請求URL給(其JSON)它解凍和顯示「NSLog(@」標記:%@「,mArrData)」時凍結「..有沒有辦法改善這種代碼?
- 我想改善喜歡做它在URL請求做了另一個線程就像ASINetworkQueue
- 我聽說GCD(大中央調度)這個代碼,但不知道它是如何或者即使是有用的..
- thanx提前
是啊,我也嘗試使用凡在我裏面requestFinished:我做我的分析有 解析和大約幾秒鐘顯示數據..過程中它仍然凍結.. 我聽說繼承了ASIHTTPRequest並在此改變requestFinished和在我打電話給[super requestFinished]之前進行解析,但是我該怎麼做?我需要在這裏與代表一起工作嗎?.. thanx可以快速回復.. – user966337
看起來您需要使用GCD在單獨的線程中運行後處理,因爲如果您使用異步請求並且它仍然阻塞用戶界面,然後問題在於你在處理時做什麼。不幸的是,我對GCD不夠了解,因此無法爲您提供任何幫助。 –