开发者

ASIHTTPRequest and ASINetworkQueue and JSON Parsing

开发者 https://www.devze.com 2023-04-08 11:06 出处:网络
// //RootViewController.m //JsonPetser33 // //Created by ME on 9/26/11. //Copyright 2011 __MyCompanyName__. All rights reserved.
//
//  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 o开发者_JAVA百科bjectWithData:[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
  • im new to objective-c and to ios.. i use the ASIHTTPRequest because it already has a httphandler..
  • the problem is when i do http request from the url given(its json) it freezes during parsing and displaying "NSLog(@"markers: %@",mArrData)".. is there a way i could improve this code?
  • i would like to improve this code like doing it in another thread just like the ASINetworkQueue done for the url request
  • i heard about gcd (Grand central dispatch) but dont know how or even if it is useful..
  • thanx in advance


Have you tried not using an ASINetworkQueue and just using a simple ASIHTTPRequest and using startAsynchronous? For example:

NSURL *url = [NSURL URLWithString:@"http://mikan-box.x10.bz/testing/json_test.php"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

If you have multiple requests you can use [request setTag:tag] to differentiate them in requestFinished:.

0

精彩评论

暂无评论...
验证码 换一张
取 消