我在Objective-C中设置了AFNetworking
的JSON帖子,并使用以下代码将数据发送到服务器:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"name": deviceName, @"model": modelName, @"pin": pin};
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"Content-Type" forHTTPHeaderField:@"application/json"];
[manager POST:@"SENSORED_OUT_URL" parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
我通过相同的请求收到信息,并希望将数据发送到NSString
。我将如何使用AFNetworking
?
答案 0 :(得分:28)
responseObject
是NSArray或NSDictionary。您可以使用isKindOfClass:
在运行时检查:
if ([responseObject isKindOfClass:[NSArray class]]) {
NSArray *responseArray = responseObject;
/* do something with responseArray */
} else if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDict = responseObject;
/* do something with responseDict */
}
如果你真的需要JSON的字符串,可以查看operation.responseString
。
答案 1 :(得分:8)
在这种情况下,当网络服务以JSON
回复时,AFNetworking
将为您执行序列化,而responseObject
很可能是NSArray
或NSDictionary
对象。
这样的对象应该比JSON
内容的字符串更有用。
答案 2 :(得分:5)
在我的情况下,它看起来像(也许它可以帮助)
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:url parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonDict = (NSDictionary *) responseObject;
//!!! here is answer (parsed from mapped JSON: {"result":"STRING"}) ->
NSString *res = [NSString stringWithFormat:@"%@", [jsonDict objectForKey:@"result"]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//....
}
];
检查响应对象的类型(如https://stackoverflow.com/a/21962445/3628317回答)
也很棒答案 3 :(得分:1)
我觉得最好将AFHTTPClient子类化为:
// MyHTTPClient.h
#import <AFNetworking/AFHTTPClient.h>
@interface MyHTTPClient : AFHTTPClient
+ (instancetype)sharedClient;
@end
// MyHTTPClient.m
#import "MyHTTPClient.h"
#import <AFNetworking/AFJSONRequestOperation.h>
static NSString *kBaseUrl = @"http://api.blah.com/yada/v1/";
@implementation MyHTTPClient
+ (instancetype)sharedClient {
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (id)init {
if (self = [super initWithBaseURL:[NSURL URLWithString:kBaseUrl]]) {
self.parameterEncoding = AFJSONParameterEncoding;
[self setDefaultHeader:@"Accept" value:@"application/json"]; // So AFJSONRequestOperation becomes eligible for requests.
[self registerHTTPOperationClass:[AFJSONRequestOperation class]]; // So that it gets used for postPath etc.
}
return self;
}
@end
重要的一点是:
然后你可以像这样使用它:
#import "MyHTTPClient.h"
@implementation UserService
+ (void)createUserWithEmail:(NSString *)email completion:(CreateUserCompletion)completion {
NSDictionary *params = @{@"email": email};
[[MyHTTPClient sharedClient] postPath:@"user" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
completion([responseObject[@"userId"] intValue], YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(0, NO);
}];
}
@end
这样做的好处在于,您的responseObject会自动被JSON解析为字典(或数组)。很干净。
(这是针对afnetworking 1.x)