2013-05-17 49 views
0

我寫了一個自定義類來處理HTTP請求,並在結果我想獲得一個NSString與BODY爲下一個請求,但是當我嘗試從此傳遞這個新創建的BODY我得到的對象是(null)。從自定義對象傳遞屬性到控制器返回(空)

但足夠的話...讓我告訴你的代碼:

這是視圖控制器代碼

#import "ViewController.h" 
#import "HTTPRequestHandler.h" 


//Definition of constants for URLs which are being used for requests 
#define GET_GRANT_KEY_URL @"http://own-dev1.railwaymen.org:4006/api/get_grant_key" 
#define PULL_USER_LOCATIONS_URL @"http://own-dev1.railwaymen.org:4006/api/pull_user_locations" 
#define ANCHOR_TERMINAL_URL @"http://own-dev1.railwaymen.org:4006/api/anchor_terminal" 

//Definition of constants for first request body 
#define GRANT_KEY_BODY @"[email protected]&password=password" 
#define PULL_USER_LOCATIONS_BODY @"user_id=b6db2184-8b16-433d-ae21-a9a415d9d4dc&grant_key=2aa1d743e7ebc2b5eb7f278f1da62b30d70c1510" 
#define ANCHOR_TERMINAL_BODY @"user_id=b6db2184-8b16-433d-ae21-a9a415d9d4dc&grant_key=2aa1d743e7ebc2b5eb7f278f1da62b30d70c1510&location_id=b91d0894-ce90-47dc-b431-4f67252310f2&client_id=2938472398492&client_secret=017305462947509274" 

@interface ViewController() 

@property (strong, nonatomic) NSMutableString *resultHTTPBody; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

- (IBAction)makeRequests { 
    //First request 
    HTTPRequestHandler *getGrantKeyRequest = [[HTTPRequestHandler alloc] initWithURL:GET_GRANT_KEY_URL 
                       body:GRANT_KEY_BODY 
                     keysToRecievie:@[@"user_id", @"grant_key"]]; 
    [getGrantKeyRequest makeConnectionWithRequest]; 

    //Second request 
    HTTPRequestHandler *pullUserLocationsRequest = [[HTTPRequestHandler alloc] initWithURL:PULL_USER_LOCATIONS_URL 
                         body:PULL_USER_LOCATIONS_BODY 
                      keysToRecievie:@[@"location_id"]]; 
    [pullUserLocationsRequest makeConnectionWithRequest]; 

    //Third request 
    HTTPRequestHandler *anchorTerminalRequest = [[HTTPRequestHandler alloc] initWithURL:ANCHOR_TERMINAL_URL 
                        body:ANCHOR_TERMINAL_BODY 
                     keysToRecievie:@[@"access_token"]]; 
    [anchorTerminalRequest makeConnectionWithRequest]; 
} 
@end 

這是從我custrom類

@interface HTTPRequestHandler() 

@property (strong, nonatomic) NSURL *requestURL; 
@property (strong, nonatomic) NSData *httpBody; 
@property (strong, nonatomic) NSMutableData *responseData; 
@property (strong, nonatomic) NSArray *keysToRecieveValues; 

@end 

@implementation HTTPRequestHandler 

- (id)initWithURL:(NSString *)url body:(NSString *)body keysToRecievie:(NSArray *)keys 
{ 
    if (self = [super init]) 
    { 
     self.responseData = [NSMutableData data]; 
     self.requestURL = [[NSURL alloc] initWithString:url]; 
     self.httpBody = [body dataUsingEncoding:NSUTF8StringEncoding]; 
     self.keysToRecieveValues = [[NSArray alloc] initWithArray:keys]; 
    } 

    return self; 
} 

- (void)makeConnectionWithRequest 
{ 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:self.requestURL]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:self.httpBody]; 
    [NSURLConnection connectionWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [self.responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.responseData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [self setRecievedData:[NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:nil]]; 
    NSLog(@"%@", [self createNewBodyWithInitializedKeys]); 
} 

- (NSString *)createNewBodyWithInitializedKeys 
{ 
    NSMutableString *tmpString = [[NSMutableString alloc] init];; 

    for (NSString *key in self.keysToRecieveValues) 
    { 
     [tmpString appendFormat:@"%@=%@?", key, [self.recievedData valueForKey:key]]; 
    } 

    [tmpString deleteCharactersInRange:NSMakeRange([tmpString length]-1, 1)]; 
    return tmpString; 
} 

@end 

而且,最後.m文件,HTTPRequestHandler頭文件:

#import <Foundation/Foundation.h> 

@interface HTTPRequestHandler : NSObject 

@property (strong, nonatomic) NSDictionary *recievedData; 

- (id)initWithURL:(NSString *)url body:(NSString *)body keysToRecievie:(NSArray *)keys; 
- (void)makeConnectionWithRequest; 
- (NSString *)createNewBodyWithInitializedKeys; 

@end 

如果你能告訴我任何提示或顯示我的錯誤,我將不勝感激。

+0

您是否嘗試過記錄responseData? – Joshua

+0

是的,我得到的是很多十六進制的東西... – cojoj

+0

什麼是HTTPRequestHandler對象傳遞mi null如果我嘗試記錄它。唯一的例外是當我從對象內部記錄它時。 – cojoj

回答

0

好的,所以我檢查了你的url的回覆(希望你不介意),看起來回復不是JSON格式,這就是爲什麼NSJSONSerialization返回零,請檢查你的服務器響應並使用在線工具驗證它,或者確保它是JSON格式。

編輯

我知道,在Xcode 4.4和@ synthesise以上是不需要了,但你無論如何都應該添加它(以防萬一),以及,如果你聲明屬性變量,你應該使用它作爲一個屬性變量(self.property),除非你在getter方法或setter方法中做了其他的東西。

+0

但NSJSONSerialization工作正常,裏面我得到我想要的,但我不能發送它的對象 – cojoj

+0

請檢查我編輯的答案 – danypata

+0

好吧,明白了,並會按照您的意見。 – cojoj

相關問題