Ios 无法在AFNetworking POST请求上设置HTTP正文
我正在尝试使用AFNetworking对http主体执行POST请求。我用了这个答案Ios 无法在AFNetworking POST请求上设置HTTP正文,ios,objective-c,post,nsurlrequest,afnetworking-2,Ios,Objective C,Post,Nsurlrequest,Afnetworking 2,我正在尝试使用AFNetworking对http主体执行POST请求。我用了这个答案 NSString *urlString = [NSString stringWithFormat:@"my-url"]; NSString *parameterData = [NSString stringWithFormat:@"request_type=get_story_nids&story_type=%@&story_number=%@", section, count];
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);
}];
我将断点放在op setCompletionBlockWithSuccess的完成块中,它甚至没有命中它
如果有帮助,做什么
NSMutableData* result = [[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] mutableCopy];
工作,并给我正确的数据
有什么想法吗?将断点添加到NSURL*url=[NSURL URLWithString:urlString];并检查您是否得到正确的url如果您得到的url为空,然后尝试以下代码
NSURL *url = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(@"url = %@",url);
我认为您没有将操作添加到操作队列。您可以在此处找到一个示例: 您还可以将AFNetworking正在处理的所有操作添加到日志中
// Add the operation to a queue
// It will start once added
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:op];