簡體   English   中英

通過AFNetworking獲取JSON null

[英]Getting JSON null from with 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]; }

我不明白為什么要向服務器發送url形式的編碼數據而不是JSON,但是希望返回JSON。 不要設置標題字段。 刪除此:

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

如果您想進行設置(雖然不必這樣做),則可以使用AFHTTPClient 例如:

[httpClient setParameterEncoding:AFJSONParameterEncoding];

此外,您確實知道AFNetworking為您編碼/解碼JSON,對嗎? 您向它傳遞一個對象,它將自動將其編碼為JSON,同樣將自動獲取接收到的JSON並將其解碼為Objective-C對象。 因此,您將永遠不會從AFNetworking庫接收純JSON,您將收到相應的對象。

編輯

我現在看到你的問題了。 您期望AFNetworking以url形式編碼一半的參數,而JSON編碼另一部分。 使用AFNetworking您必須將整個參數對象編碼為一件事或另一件事。 我建議您通過保留創建的homePostParams並使用JSON對其進行編碼,將其更改為100%JSON,然后重寫服務器以獲取所有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形式編碼,那就更好了

我使用的是AFNetworking庫,但是您似乎缺少了一些我要做的步驟。 我通常將AFHTTPClient子類化。 無論您是否子類化,請確保調用AFHTTPClient的registerHTTPOperationClass方法。

例如,在初始化過程中,在我的AFHTTPClient子類中,我這樣做(其中self是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 }];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM