【问题标题】:AFNetworking Expected content type "application/xml", "text/xml", text/plainAFNetworking 预期的内容类型“application/xml”、“text/xml”、text/plain
【发布时间】:2012-06-10 00:50:21
【问题描述】:

我正在尝试使用 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

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

//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);
     }];

我将默认标题设置为

[self setDefaultHeader:@"Content-type" value:@"text/plain"];
    [self setDefaultHeader:@"Accept" value:@"text/plain"];

所以我不确定为什么它仍然认为它需要 xml?

【问题讨论】:

    标签: content-type afnetworking


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      嗨,马特,所以我刚刚开始测试这个。看起来我注册了错误的 AF 操作。我变了

      [self registerHTTPOperationClass:[AFXMLRequestOperation class]]; 
      

      [self registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 
      

      一切都很好!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-03
        • 2012-05-16
        • 2010-11-05
        • 1970-01-01
        • 2014-10-25
        • 1970-01-01
        • 2018-03-22
        • 1970-01-01
        相关资源
        最近更新 更多