0

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

これは、私のカスタム クラスの .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

ヒントや私の間違いを教えていただければ幸いです。

4

1 に答える 1

0

わかりましたので、あなたの URL の応答を確認しました (気にしないでください)。応答が JSON 形式ではないようです。そのため、NSJSONSerializationnil が返されます。サーバーの応答を確認し、オンライン ツールなどで検証してください。 JSON形式です。

編集

xcode 4.4以降では@ synthesise不要になったことはわかっていますが、とにかく追加する必要があります(念のため)。また、プロパティ変数を宣言する場合は、プロパティ変数(self.property)として使用する必要があります。ゲッターメソッドまたはセッターメソッドで他のことをしています。

于 2013-05-17T08:57:25.150 に答える