JSON空我寫了兩個代碼都是相同充分利用與AFNetworking
- 使用NSURLConnection的和
- 使用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]; }
謝謝你的迴應,但沒有運氣。 – AAV
@AmitVyawahare查看我的編輯。你期待原始的JSON,但獲得一個對象? –
@AmitVyawahare我現在看到你爲什麼在做表單編碼,我可以建議,而不是表單編碼你做100%的JSON。問題在於AFNetworking會將整個homePostParams編碼爲JSON或不編碼。你期待它是一半編碼和一半JSON編碼。所以,如果你想要服務器接收任何JSON,它必須是100%JSON,或者你必須使用與上面非常相似的編碼方案,但這幾乎違背了「AFNetworking」的目的。 –