2013-05-07 67 views
0

我想從JSON響應中獲取解析對象並在方法中使用,當我通常這樣做時,我可以在此方法後使用parsedObject [self.delegate requestJSONFinishedWithParsedObject:parsedObject];我使用,但現在我想要這個parsedObject在我想要的地方。我的意思是調用一個方法,並在返回中有對象。異步後發送請求objective-c後返回parsedobject IOS

像這樣:

的NSDictionary * parsedObject = [self.delegate responseJsonDictionary]; 在我需要的課程中使用它。

我的問題是,我有一個@property (strong, nonatomic) NSMutableArray *projectsArray;,我想在使用它之前完整填充此屬性。我正在創建一個包含JSON項目的動態菜單,但是當來自JSON的數據位於self.projectsArray時,我的菜單已經創建並且json的對象沒有出現。

我使用這張幻燈片菜單:https://github.com/andrewroycarter/SlideViewController

我使用這個類來管理所有的請求和它完美的作品。

RequestJSON.m

-(void) requestWithURL:(NSDictionary *)jsonDictionary :(NSString*)myURL :(NSString *)method 
{ 


    NSURL *thisURL = [NSURL URLWithString:myURL]; 


    NSString *__jsonString; 
    NSData *__jsonData; 
    if([NSJSONSerialization isValidJSONObject:jsonDictionary]) 
    { 
     __jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil]; 
     __jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding]; 
    } 



    // Be sure to properly escape your url string. 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:thisURL]; 
    [request setHTTPMethod:method]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue:@"Carlos" forHTTPHeaderField:@"User"]; 

    if([method isEqual: @"POST"]){ 
    [request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
     [request setHTTPBody: __jsonData]; 

    } 
    NSLog(@"llega= asdasdasd %@, %@, %@",method,jsonDictionary,myURL); 

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 


} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

    //INicializo variable NSMutableData 
    self.myConnectionData= [NSMutableData data]; 


    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
    NSDictionary *myResponse = [[NSDictionary alloc] init]; 
    myResponse=[httpResponse allHeaderFields]; 
    statusCode = [httpResponse statusCode]; 

} 


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.myConnectionData appendData:data]; 
} 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [self.delegate requestJSONFailed]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    NSError *thisError; 


    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:myConnectionData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&thisError]; 

    [self.delegate requestJSONFinishedWithParsedObject:parsedObject]; 
} 

@end 

RequestJSON.h

@protocol RequestJSONDelegate; 

@interface RequestJSON : NSObject 
{ 
    NSInteger statusCode; 

} 

@property (nonatomic,assign) id <RequestJSONDelegate> delegate; 
@property (nonatomic, strong) NSMutableData *myConnectionData; 

-(void) requestWithURL:(NSDictionary *) params:(NSString*)myURL :(NSString *)method; 
-(void) requestForLogin:(NSString*)myURL :(NSString*)method :(NSString*)pass; 

@end 


@protocol RequestJSONDelegate 

-(void) requestJSONFinishedWithParsedObject:(NSDictionary*)parsedObject; 

-(void) requestJSONFailed; 

@end 

在我想這些數據的其他類我用這個方法

-(void) requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject{ 

    NSArray *projectsObjectArray = [parsedObject objectForKey:@"projects"]; 
} 

-(void) requestJSONFailed{} 

並開始請求我在我的主類中使用了這個。

example.m

RequestJSON *requestJSON = [ [RequestJSON alloc] init]; 
    [requestJSON requestWithURL:(NSDictionary *) params:url :method]; 
    [requestJSON setDelegate:self]; 

謝謝

回答

0

您的要求是異步的,所以你應該處理的情況下,你的菜單的內容還沒有準備好,並表現出一定的加載UI就像一個活動的指標如果數據還沒有準備好,並顯示錯誤,例如使用alertView,以防請求失敗...

因此,當您在viewDidLoad或viewWillAppear中創建您的UIViewController,或者哪裏更適合您的應用程序時應該che CK,如果你已經顯示,如果沒有顯示你的加載UI(我希望你正在使用某種形式的緩存,而不是要求這些數據的所有次)

實現你的協議

,如果你有一個數據成功

- (void)requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject { 

    //remove the loading UI 
    [self removeLoadingView]; // your custom method to remove the loading UI 

    // populate your data source and reload the content 
    self.projectsArray = [parsedObject objectForKey:@"projects"]; 

    // this in case you are using a tableview for your menu 
    [self.tableView reloadData]; 
} 

失敗的情況下

- (void)requestJSONFailed { 

    //remove the loading UI 
    [self removeLoadingView]; // your custom method to remove the loading UI 

    // show an error feedback like an alertview 
    [self showError]; // your custom method to display errors 
} 

,這可能是一種方法來處理負載,我不建議你使用同步請求,該W虐待停止你的用戶界面,直到請求完成了一個非常糟糕的用戶體驗

+0

嗨馬努,我已經做了你所說的,甚至我已經把更多的[_tableView reloadData]在我的代碼。問題是當我嘗試爲我的部分使用數組時,它仍然是空的。我正在使用這個菜單[鏈接](https://github.com/andrewroycarter/SlideViewController)當我使用projectsArray時,我在MySlideViewController.m中創建了一個新節,但是當我登錄時 - (void)requestJSONFinishedWithParsedObject :(NSDictionary *)parsedObject'self.projectsArray它的完整..謝謝 – roxeman3 2013-05-07 18:09:16

+0

非常奇怪...首先嚐試找到問題,我建議你做一個測試,沒有任何遠程請求,用你的數據初始化你的數組viewController希望能夠正常工作,並在UITableView委託方法中逐步檢查一切正常。之後,你做了檢查你的方法 - (無效)requestJSONFinishedWithParsedObject:(NSDictionary *)parsedObject接收到的數據具有預期的格式 – Manu 2013-05-08 08:34:02

+0

我也這樣做。如果我創建一個數組,它的作品。問題是我收到requestJSONFinishedWithPasedObject方法中的信息,如果我用那裏的信息完成數組,則第一個方法中的數組是空的。問題是我創建之前self.projectArray已滿的部分..甚至重新加載tableview我不能看這個數據。 – roxeman3 2013-05-08 11:04:13