私は2つのコードを書きました両方とも同じです
- NSURLConnectionを使用して
- AFNetworkingを使用します。
私のNSURLConnection
コードは正常に機能しますが、AFNetworking
コードは機能しません。問題は、コードからJSON応答を取得しているのに、コードから応答をNSURLConnection
取得していないのに、応答コードを200として取得していることです。私はすべての可能な組み合わせを試しましたが、それでも運がありません。何が問題なのかわかりません。今後も使いたいので、誰か助けてください。ありがとう、-アミットJSON
AFNetworking
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]; }