[英]Unable to set HTTP body on AFNetworking POST request
I'm trying to do a POST request with a http body using AFNetworking. 我正在尝试使用AFNetworking与http正文进行POST请求。 I used this post's answer 我用这个帖子的答案
NSString *urlString = [NSString stringWithFormat:@"my-url"];
NSString *parameterData = [NSString stringWithFormat:@"request_type=get_story_nids&story_type=%@&story_number=%@", section, count];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[parameterData dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id result) {
completionBlock(nids,nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock(nil, error);
}];
I put breakpoints in the completion blocks of op setCompletionBlockWithSuccess and it doesn't even hit it. 我在op setCompletionBlockWithSuccess的完成块中放置了断点,它甚至没有击中它。
If it helps, doing 如果有帮助,那就做
NSMutableData* result = [[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] mutableCopy];
works and gives me the right data. 可以给我正确的数据。
Any ideas? 有任何想法吗?
add break point to NSURL *url = [NSURL URLWithString:urlString]; 将断点添加到NSURL * url = [NSURL URLWithString:urlString]; and check do you get proper url if you get url null then try following code 并检查是否获得正确的URL(如果您获得的URL为空),然后尝试以下代码
NSURL *url = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(@"url = %@",url);
I think you missed to add your operation to a operation queue. 我认为您错过了将您的操作添加到操作队列的操作。 You can find an example here: http://samwize.com/2013/03/02/queue-http-operations-with-afnetworking/ 您可以在此处找到示例: http : //samwize.com/2013/03/02/queue-http-operations-with-afnetworking/
// Add the operation to a queue
// It will start once added
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:op];
You can also add https://github.com/AFNetworking/AFHTTPRequestOperationLogger to log all operations AFNetworking is dealing with. 您还可以添加https://github.com/AFNetworking/AFHTTPRequestOperationLogger来记录AFNetworking正在处理的所有操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.