我编写了一个自定义类来处理 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
这是来自我的 cusstrom 类的 .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
如果您能告诉我任何提示或显示我的错误,我将不胜感激。