gpt4 book ai didi

objective-c - 使用 AFNetworking 从中获取 JSON null

转载 作者:行者123 更新时间:2023-11-28 19:17:50 30 4
gpt4 key购买 nike

我写的两段代码都是一样的

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

我的 NSURLConnection 代码工作正常,但 AFNetworking 代码不行。问题是我从我的 NSURLConnection 代码得到 JSON 响应,但我没有从 AFNetworking 代码得到 JSON 响应,但我得到的响应代码为 200 . .我已经尝试了所有可能的组合,但仍然没有运气。我不知道是什么问题。我想继续使用 AFNetworking 所以请有人帮助我。谢谢,——阿米特

NSURL连接:

- (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]; }

AF网络:

- (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]; }

最佳答案

我使用 AFNetworking 库,但您似乎缺少我执行的一些步骤。我通常将 AFHTTPClient 子类化。无论您是否子类化,请务必调用 AFHTTPClient 的 registerHTTPOperationClass 方法。

例如,在初始化过程中我的 AFHTTPClient 子类中,我这样做(其中 self 是 AFHTTPClient 实例):

[self registerHTTPOperationClass:[AFJSONRequestOperation class]];

这将为您设置适当的接受 header 。

对于操作(所有期望 json 响应),我会做类似的事情:

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

关于objective-c - 使用 AFNetworking 从中获取 JSON null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11145263/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com