IOS 开发AFNetworking 3.0+ 遇到的问题
2016-05-18 本文已影响993人
John_LS
问题1:
接收不到服务器的数据:
添加代码:
//如果报接受类型不一致请替换一致text/html或别的
_sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@”text/html”];
问题2:
服务器接收不到参数
请求参数确保和你选择的格式一致。
//申明返回的结果是json类型
_sharedClient.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
_sharedClient.requestSerializer=[AFJSONRequestSerializer serializer];
如果不确定是什么格式,把这两句话注释掉就行。
更新 其他(以下代码并不是3.0+)
我们在开发过程中,经常会遇到有些页面不止一个网络请求,有时候需要两个三个甚至更多,这个时候我们就需要队列请求,下边是GET请求的多个请求放在队列里边:
[objc] view plain copy print?
NSURL *url = [NSURL URLWithString:@”http://www.baidu.com“];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
NSURL *url2 = [NSURL URLWithString:@"http://www.sohu.com"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response2: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
NSURL *url3 = [NSURL URLWithString:@"http://www.sina.com"];
NSURLRequest *request3 = [NSURLRequest requestWithURL:url3];
AFHTTPRequestOperation *operation3 = [[AFHTTPRequestOperation alloc] initWithRequest:request3];
[operation3 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response3: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
//同时请求
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
//operation2 在 operation1 请求完成后执行
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operation2 addDependency:operation1];
[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO];
下边是POST请求:
[objc] view plain copy print?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@”https://gowalla.com/friendships/request?user_id=1699“]];
[request setHTTPMethod:@”POST”];
NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@”Token token=\”%@\”“, kOAuthToken] forKey:@”Authorization”];
[request setAllHTTPHeaderFields:headers];
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
BOOL HTTPStatusCodeIsAcceptable = [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:[response statusCode]];
if (HTTPStatusCodeIsAcceptable) {
NSLog(@”Friend Request Sent”);
} else {
NSLog(@”[Error]: (%@ %@) %@”, [request HTTPMethod], [[request URL] relativePath], error);
}
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];