AFNetworking预期内容类型"application/xml","text/xml",text/plain

Ant*_*rio 0 content-type afnetworking

我试图使用AfNetworking AfHttpClient登录谷歌阅读器,但我得到这个错误,我可以;似乎弄清楚.

下面是我的AFNetworking的子类:

// main url endpoints
#define GOOGLE_ACCOUNTS_BASE_URL @"https://www.google.com/accounts/"

@implementation ADPGoogleLoginClient

+ (ADPGoogleLoginClient *)sharedClient {
    static ADPGoogleLoginClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[ADPGoogleLoginClient alloc] initWithBaseURL:[NSURL     URLWithString:GOOGLE_ACCOUNTS_BASE_URL]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFXMLRequestOperation class]];

    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
    [self setDefaultHeader:@"Content-type" value:@"text/plain"];
    [self setDefaultHeader:@"Accept" value:@"text/plain"];

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下代码形成请求:

//set up request params
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"googlereader-ios-client", @"client",
                            [keychainCredentials objectForKey:(__bridge id)kSecAttrAccount], @"Email",
                            [keychainCredentials objectForKey:(__bridge id)kSecValueData], @"Passwd",
                            @"reader", @"service",
                            @"ipad", @"source", nil];

    //make requests
    [[ADPGoogleLoginClient sharedClient] getPath:@"ClientLogin" 
                                      parameters:params
                                         success:^(AFHTTPRequestOperation *operation , id responseObject)
     {
         //parse out token and store in keychain
         NSString* responseString = [operation responseString];  
         NSString* authToken = [[[responseString componentsSeparatedByString:@"\n"] objectAtIndex:2] 
                                stringByReplacingOccurrencesOfString:@"Auth=" withString:@""];

         keychainToken = [[KeychainItemWrapper alloc] initWithIdentifier:@"GReaderToken" accessGroup:nil]; 
         [keychainToken setObject:authToken forKey:(__bridge id)kSecValueData]; 

         loginSuccess();

     } 
           failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     {
         NSLog(@"There was an error logging into Reader - %@", [error localizedDescription]);

         loginFailure(error);
     }];
Run Code Online (Sandbox Code Playgroud)

我将默认标头设置为

[self setDefaultHeader:@"Content-type" value:@"text/plain"];
    [self setDefaultHeader:@"Accept" value:@"text/plain"];
Run Code Online (Sandbox Code Playgroud)

所以我不知道为什么它仍然认为它是期待xml?

mat*_*ttt 6

您正在设置Content-Type请求的标头,但您看到的错误来自响应.为了使请求被视为"成功",内容类型需要与期望相匹配(以避免在您期望XML时尝试解析JSON响应).

在相同的错误代码中,它应该提到它实际返回的内容类型.如果它确实是XML,请使用添加AFXMLRequestOperation +addAcceptableContentTypes:,并且一切都应该正常工作.