ASIHTTPRequest 和 ASINetworkQueue 以及 JSON 解析

Posted

技术标签:

【中文标题】ASIHTTPRequest 和 ASINetworkQueue 以及 JSON 解析【英文标题】:ASIHTTPRequest and ASINetworkQueue and JSON Parsing 【发布时间】:2011-09-27 06:29:50 【问题描述】:
//
//  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),但不知道它是否有用,甚至不知道它是否有用.. 提前感谢

【问题讨论】:

【参考方案1】:

您是否尝试过不使用 ASINetworkQueue 而只使用简单的 ASIHTTPRequest 并使用 startAsynchronous?例如:

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

如果您有多个请求,您可以使用[request setTag:tag]requestFinished: 中区分它们。

【讨论】:

是的,我确实尝试在我的 requestFinished 内部使用它:我在那里进行解析,它在解析和显示数据期间仍然冻结.. 大约几秒钟.. 我听说将 ASIHTTPRequest 子类化并覆盖 requestFinished并在我调用 [super requestFinished] 之前在那里进行解析.. 但我该怎么做呢?我需要在这里与代表一起工作吗?..thanx 虽然可以快速回复.. 看起来您需要使用 GCD 在单独的线程中运行后处理,因为如果您使用的是异步请求并且它仍然阻塞 UI,那么问题就出在您的任何'正在做的同时处理。不幸的是,我对 GCD 的了解不够,无法在这部分为您提供任何帮助,抱歉!

以上是关于ASIHTTPRequest 和 ASINetworkQueue 以及 JSON 解析的主要内容,如果未能解决你的问题,请参考以下文章

ASIHTTPRequest类库简介和使用说明

ASIHTTPRequest 和 userInfo

IOS-网络(ASIHTTPRequest的使用简介)

ASIHTTPRequest 与 Put 问题和 kCFErrorDomainCFNetwork 错误 303

ASIHTTPRequest 和 ASINetworkQueue 以及 JSON 解析

IOS开发 REST请求 ASIHTTPRequest用法