2012-02-03 26 views
5

我想從我的哈德森url地址獲取JSON並使用HTTP驗證身份驗證我的(Mac OS X)應用程序。通過使用AFNetworking的HTTP身份驗證獲取JSON

繼我使用的例子:

// AppDelegate.m 
- (void) doSomething { 
    [[CommAPIClient sharedClient] getPath:@"/computer/api/json" parameters:nil 
success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Name: %@", [responseObject valueForKeyPath:@"totalExecutors"]); 

    } failure:nil]; 
} 

// CommAPIClient.m 
+ (CommAPIClient *) sharedClient { 
    static CommAPIClient *_sharedClient = nil; 
    static dispatch_once_t onceToken; 

    dispatch_once(&onceToken, ^{ 
     AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; 
     _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString: [appDelegate.hudsonTextField stringValue]]]; 
    }); 
    return _sharedClient; 
} 

- (id) initWithBaseURL:(NSURL *)url { 
    self = [super initWithBaseURL:url]; 
    if (self){ 
     [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

     AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; 
     userName = [appDelegate.userTextField stringValue]; 
     password = [appDelegate.passwdTextField stringValue]; 

     [self setAuthorizationHeaderWithUsername:userName password:password]; 
     [self setDefaultHeader:@"Accept" value:@"application/json"]; 
    } 
    return self; 
} 

我想要得到的計算機列表在我的下拉列表顯示,但此兩行不一起工作: [自setAuthorizationHeaderWithUsername:用戶名密碼:密碼]。 [self setDefaultHeader:@「Accept」value:@「application/json」];

如果我只是用第一行,我authetication的作品,但我收到的錯誤,因爲我試圖得到一個關鍵:

2012-02-03 02:43:57.542 HudsonSlave[7523:707] An uncaught exception was raised 
2012-02-03 02:43:57.542 HudsonSlave[7523:707] [<NSConcreteData 0x100850000> valueForUndefinedKey:]: this class is not key value coding-compliant for the key totalExecutors. 

2012-02-03 02:43:57.623 HudsonSlave[7523:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSConcreteData 0x100850000> valueForUndefinedKey:]: this class is not key value coding-compliant for the key totalExecutors.' 

如果使用第二線,我的身份驗證將返回一個錯誤403。

任何人都可以幫助解決問題嗎?

謝謝併爲任何英文錯誤表示歉意。

蒂亞戈

+0

當你同時使用兩條線時會發生什麼? – mattt 2012-02-03 05:14:38

+0

@mattt,錯誤告訴我缺少內容類型,所以我添加了第三行: [self setDefaultHeader:@「Content-Type」value:@「application/json」]; 和錯誤是: 錯誤域= com.alamofire.networking.error代碼= -1016「預期內容類型{( 「文本/ JavaScript的」, 「應用/ JSON」, 「文本/ JSON」 )},得到了application/javascript「UserInfo = 0x100513190 {NSLocalizedDescription =期望的內容類型{( 」text/javascript「, 」application/json「, 」text/json「 )},獲取應用/ javascript,NSErrorFailingURLKey = http://hudson.concretecorp.com.br/computer/api/json} – unnamedd 2012-02-03 05:39:17

回答

7

「預期的內容類型{( 」文/ JavaScript的「, 」應用/ JSON「, 」文/ JSON「)},得到了應用程序/ JavaScript的」

就像該錯誤說,請求期待text/javascript,application/jsontext/json之一,但服務器發送application/javascript(這是JSON的不正確的MIME類型)。

要麼讓您的服務器以正確的MIME類型進行響應,要麼手動更改AFJSONRequestOperation中用於指定哪些MIME類型可訪問的行。對於改變涉及子類的庫代碼來說,有一個更簡單的辦法,但這可能比你的情況更麻煩。

+0

請注意:你不需要在'AFHTTPCLient'中設置Content-Type頭。這將根據您的請求參數序列化的方式自動設置。 – mattt 2012-02-03 06:25:00

14

我遇到了這個問題與Flickr的API和最終的子類AFJSONRequestOperation結果是微不足道的。你只需要重寫類方法defaultAcceptableContentTypes正是如此,例如:

+ (NSSet *)defaultAcceptableContentTypes; 
{ 
    return [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil]; 
} 

然後在我的AFHTTPClient子,我剛註冊我的子類作爲默認的HTTP操作類:

[self registerHTTPOperationClass:[CCFFlickrJSONRequestOperation class]]; 

UPDATE 2013-09- 09 14-52-47:

方法名是現在+ (NSSet *)acceptableContentTypes

+0

在嘗試了很多解決方案之後,子類化AFJSONRequestOperation並重寫defaultAcceptableContentTypes是最乾淨和更好的解決方法!謝謝! – DZenBot 2012-06-10 18:11:14

+5

它看起來像在'AFNetworking 1.0RC1'這個方法名稱已經改爲'+(NSSet *)acceptableContentTypes' – bearMountain 2012-08-19 00:30:10

+0

備註:有時還需要添加@「text/html」 – BabyPanda 2012-09-19 04:27:23

4

如果這是一個一次性的事情,而不是繼承整個AFJSON的RequestOperation對象,你纔可以直接設定可接受的內容類型,無論你需要:

AFJSONRequestOperation *operation = blah blah blah 

[operation setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html", nil]]; 

[operation start]; 
+0

setAcceptableContentTypes似乎不存在AFNetworking 1.1 – 2013-01-23 18:06:58

5

其實你現在可以輕鬆地添加更多的內容類型:

[AFHTTPRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]]; 
+2

正確的地方放在哪裏? – Shmidt 2013-07-03 19:38:43

+0

好問題,Shmidt。 – Morkrom 2013-07-17 00:29:13

2

由於AFNetworking 2。0,序列化不再是RequestOperation類的一部分,並委派給Serializer類。如果你使用2.0,你想要做這樣的事情(通常我認爲這是最好的解決方案,而不是取代它 - 你永遠不會知道它將在未來的AFNetworking SDK版本中有什麼基本值):

AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager]; 

NSMutableSet *contentTypes = [requestManager.responseSerializer.acceptableContentTypes mutableCopy]; 
[contentTypes addObject:@"text/html"]; 
[contentTypes addObject:@"text/plain"]; 
// ... etc ... 
requestManager.responseSerializer.acceptableContentTypes = [contentTypes copy]; 

如果您決定直接使用AFHTTPRequestOperation對象,只需將「requestManager.responseSerializer」替換爲「myOperationObject.responseSerializer」即可。

+0

感謝bro.working – 2015-05-21 04:24:06

相關問題