2011-09-27 2 views
1
은 이미 HttpHandler를이 있기 때문에
// 
// 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 
  • 이 목표 - C와 IOS에 메신저의 새로운 .. 내가 ASIHTTPRequest을 사용 ..
  • 문제입니다 "NSLog (@"표시 자 : % @ ", mArrData)"를 파싱 및 표시하는 동안 동결 상태가 발생했습니다.이 코드를 개선 할 수있는 방법이 있습니까?
  • 나는 GCD (그랜드 센트럴 파견)에 대해 들어
  • URL 요청에 대해 수행 다만 ASINetworkQueue 같은 다른 스레드에서 그 일을 같은 코드를 개선하기 좋아하지만 .. 유용 얼마나 심지어 알고
  • 해달라고 것

답변

0

사전에서

  • 고맙습니다 당신이 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]; 
    

    당신은 requestFinished:에서 그들을 구별하는 [request setTag:tag]를 사용할 수 있습니다.

  • +0

    그래 내가 어디에서 내 requestFinished 내부의 것을 사용해보십시오 않았다 내가 ASIHTTPRequest을 서브 클래스에 대해 들어 난 내 분석을이 여전히 분석하고 몇 초에 대한 데이터 ..를 표시하는 동안 정지 .. 및 requestFinished를 오버라이드 (override)하는과 [super requestFinished]를 호출하기 전에 거기에 파싱을해라.하지만 어떻게해야합니까? 위임자와 함께 작업해야합니까? .. 고맙습니다. 고맙습니다. – user966337

    +0

    비동기 요청을 사용하는 경우 GCD를 사용하여 별도의 스레드에서 사후 처리를 실행해야합니다. 여전히 UI를 차단하고 있다면 문제는 처리하는 동안 무엇을하든간에 있습니다. 불행히도 나는 GCD에 대해 당신에게 그 부분에 대한 도움을주기에는 충분하지 못하다. 미안하다. –