2012-06-21 121 views
0

JSON空我寫了兩個代碼都是相同充分利用與AFNetworking

  1. 使用NSURLConnection的和
  2. 使用AFNetworking。

我的NSURLConnection代碼工作正常,但AFNetworking代碼沒有。問題是我從我的NSURLConnection代碼獲得JSON響應,但我沒有從AFNetworking代碼獲得JSON響應,但我得到的響應代碼爲200。我嘗試了所有可能的組合,但仍然沒有運氣。我不知道是什麼問題。我想用AFNetworking前進,所以請別人幫助我。謝謝你, - 阿米特

NSURLConnection的:

- (IBAction)connectHandler:(UIButton *)sender { 

//This is where we attempt to connect to the URL represented by self.labelHostURL 
//along with exception handling 
NSString *request_type = @"STARTUP";  
NSString *request_data = @"{\"lstCacheRefDt\":\"10/10/2010\",\"langCd\":\"en_US\",\"timeZn\":\"America/Chicago\",\"brndId\":\"WM\",\"prgmId\":\"WM0012\"}"; 
NSLog(@"Request: %@", request_data); 

NSURL *url = [NSURL URLWithString:[ self.labelHostURL text ]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
//NSData *requestData = [NSData dataWithBytes:[request_data UTF8String] length:[jsonRequest length]]; 

[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

NSString* request_body = [NSString 
          stringWithFormat:@"request_type=%@&request_data=%@", 
          [request_type stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
          [request_data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

[request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]]; 

[NSURLConnection connectionWithRequest:request delegate:self]; } 

AFNetworking:

- (void) getHomeData { 

NSURL *url = [[NSURL alloc] initWithString:@"https://localhost:8443/MNMMLMockService/mAccountsWeb/services/mnapp/rpc"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

NSDictionary *homePostParams1 = [NSDictionary dictionaryWithObjectsAndKeys: 
           @"10/10/2010", @"lstCacheRefDt", 
           @"en_US", @"langCd", 
           @"America/Chicago", @"timeZn", 
           @"WM", @"brndId", 
           @"WM0012", @"prgmId", nil]; 

NSDictionary *homePostParams = [NSDictionary dictionaryWithObjectsAndKeys:@"STARTUP", @"request_type", homePostParams1,@"request_data", nil]; 

NSLog(@"Dic: %@", homePostParams); 

NSMutableURLRequest *homeRequest = [httpClient requestWithMethod:@"POST" path:@"" parameters:homePostParams]; 
[homeRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[homeRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:homeRequest 
            success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
             NSLog(@"JSON %@", JSON); 
             NSLog(@"operation StatusCode: %d", [response statusCode]); 
            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
             NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
            }]; 
[operation start]; } 

回答

2

我使用AFNetworking庫,但您似乎缺少一些我所做的步驟。我通常子類AFHTTPClient。無論你是否繼承子類,都要確保調用AFHTTPClient的registerHTTPOperationClass方法。

例如,在過程中,init進程我AFHTTPClient子,怎麼辦?(這裏的自我是AFHTTPClient實例):

[self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

這將設置相應的接受頭你。

對於操作(所有期望JSON響應),我做這樣的事情:

AFHTTPRequestOperation *httpOperation = [httpClient HTTPRequestOperationWithRequest:request 
    success:^(AFHTTPRequestOperation *operation, id responseJSONObject) { // some logic} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) { // some logic }]; 
2

我不明白你爲什麼要發送URL形式編碼數據,而不是JSON的服務器,但預計JSON回來。不要設置標題字段。如果你想設置(雖然你不應該)你AFHTTPClient

[homeRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[homeRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

:刪除此。例如:

[httpClient setParameterEncoding:AFJSONParameterEncoding]; 

此外,你知道AFNetworking爲你編碼/解碼JSON,對吧?您將它傳遞給一個對象,它會自動將其編碼爲JSON,並同樣會自動接收JSON並將其解碼爲一個objective-c對象。所以你永遠不會收到來自AFNetworking庫的純JSON,你會收到相應的對象。

編輯

現在我看到你的問題。您期望AFNetworking以url形式對參數的一半進行編碼,而JSON則對另一部分進行編碼。使用AFNetworking,您必須將整個參數對象編碼爲一個或另一個。我建議你通過保留你創建的homePostParams並用JSON對其進行編碼,然後重寫服務器以期望所有的JSON,來改變爲100%的JSON。

如果你必須像這樣分裂,你不能依靠AFNetworking來爲你做編碼/解碼。你必須做你與NSURLConnection做了什麼:失敗的

NSString *request_data = @"{\"lstCacheRefDt\":\"10/10/2010\",\"langCd\":\"en_US\",\"timeZn\":\"America/Chicago\",\"brndId\":\"WM\",\"prgmId\":\"WM0012\"}"; 
NSDictionary *homePostParams = [NSDictionary dictionaryWithObjectsAndKeys:@"STARTUP", @"request_type", request_data,@"request_data", nil]; 

AFNetworking目的雖然。如果服務器只是希望接收JSON而不是url格式編碼,會更好一些

+0

謝謝你的迴應,但沒有運氣。 – AAV

+0

@AmitVyawahare查看我的編輯。你期待原始的JSON,但獲得一個對象? –

+1

@AmitVyawahare我現在看到你爲什麼在做表單編碼,我可以建議,而不是表單編碼你做100%的JSON。問題在於AFNetworking會將整個homePostParams編碼爲JSON或不編碼。你期待它是一半編碼和一半JSON編碼。所以,如果你想要服務器接收任何JSON,它必須是100%JSON,或者你必須使用與上面非常相似的編碼方案,但這幾乎違背了「AFNetworking」的目的。 –