将属性从自定义对象传递给控制器​​返回(null)

时间:2013-05-17 08:31:40

标签: iphone ios nsurlconnection httprequest

我编写了一个自定义类来处理HTTP请求,结果我希望得到一个带有BODY的NSString用于下一个请求,但是当我尝试从这个对象传递这个新创建的BODY时,我得到的是(null)。

但足够的话......让我告诉你代码:

这是ViewController代码

#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=vergun@gmail.com&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类的.m文件

@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

最后,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

如果你能告诉我任何提示或证明我的错误,我将非常感激。

1 个答案:

答案 0 :(得分:0)

好的,所以我检查了你网址的回复(希望你不介意),似乎回复不是JSON格式,这就是NSJSONSerialization返回nil的原因,请检查你的服务器响应并用在线工具或其他东西,以确保它是一种JSON格式。

修改

我知道在xcode 4.4及更高版本中不再需要@ synthesise,但你应该添加它(以防万一),如果你声明一个属性变量,你应该将它用作一个属性变量(self.property)除非你在getter方法或setter方法中做了一些其他的事情。