2014-09-06 102 views
0

我有下面的代碼使用一個相當古老的庫稱爲ASIHTTPRequest。我期待升級圖書館,但需要一些幫助,以確保我正確地做到這一點,並讓我的腳溼潤。這是我需要轉換爲AFNetworking的代碼。任何提示或建議表示讚賞。AFNetworking:如何將此標頭請求轉換爲使用AFNetworking?

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]]; 
[request setDelegate:self]; 

NSMutableDictionary *userinfo = [[NSMutableDictionary alloc] init]; 
[userinfo setObject:NSStringFromSelector(self.didFinishSelector) forKey:@"didFinishSelector"]; 
[userinfo setObject:NSStringFromSelector(self.didFailSelector) forKey:@"didFailSelector"]; 

[request setUserInfo:userinfo]; 
[request setUserAgentString:USER_AGENT]; 
[request setTimeOutSeconds:60]; 
[request addRequestHeader:@"apiKey" value:apiKey]; 
[request setRequestMethod:method]; 
+0

你需要用'AFNetworking'來做'POST'嗎? – iphonic 2014-09-06 18:15:05

+0

是的,我需要做POST。 – user3606054 2014-09-06 18:19:12

回答

0

隨着AFNetworking代碼應如下

NSDictionary *[email protected]{"key":value};//The values you want to POST arrange in dictionary. 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

    [manager.requestSerializer setValue:apiKey forHTTPHeaderField:@"apiKey"]; 
    [manager.requestSerializer setValue:USER_AGENT forHTTPHeaderField:@"User-Agent"]; 

    [manager.requestSerializer setTimeoutInterval:60]; 



    [manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Result %@",responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"%@",error.description); 

    }]; 

我希望它能幫助。乾杯。