Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法在AF3.0中设置正文_Ios_Objective C_Afnetworking 3 - Fatal编程技术网

Ios 无法在AF3.0中设置正文

Ios 无法在AF3.0中设置正文,ios,objective-c,afnetworking-3,Ios,Objective C,Afnetworking 3,我需要将{“token”:“asdfwrwer234d”}设置为http主体 self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; [self.requestSerializer setValue:Token forHTTPHeaderField:@"Token"]; [self POST:[NSString stringWithFormat:@"%@2/9/1",BaseUR

我需要将{“token”:“asdfwrwer234d”}设置为http主体

self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[self.requestSerializer setValue:Token forHTTPHeaderField:@"Token"];

[self POST:[NSString stringWithFormat:@"%@2/9/1",BaseURL] parameters:nil
  progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    completion(responseObject,YES);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    completion(error,NO);

}];

它解决了我的问题:

NSDictionary *body = @{@"token":Token};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];


AFHTTPResponseSerializer *responseManager = [AFHTTPResponseSerializer serializer];

responseManager.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

manager.responseSerializer = responseManager;

NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@2/9/1",BaseURL] parameters:nil error:nil];

req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];


[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

    if (!error) {
        NSError* error;
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
                                                             options:kNilOptions
                                                               error:&error];

    } else {
        NSLog(@"Error: %@, %@, %@", error, response, responseObject);
    }
}] resume];

它解决了我的问题:

NSDictionary *body = @{@"token":Token};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];


AFHTTPResponseSerializer *responseManager = [AFHTTPResponseSerializer serializer];

responseManager.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

manager.responseSerializer = responseManager;

NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@2/9/1",BaseURL] parameters:nil error:nil];

req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];


[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

    if (!error) {
        NSError* error;
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
                                                             options:kNilOptions
                                                               error:&error];

    } else {
        NSLog(@"Error: %@, %@, %@", error, response, responseObject);
    }
}] resume];

你有什么问题?它没有设置为http主体它添加为http头,我需要一种方法来设置HttpBody。你有什么问题?它没有设置为http主体它添加为http头,我需要一种方法来设置HttpBody。