Objective c 使用AFNetworking从中获取JSON null
我写的两个代码都是一样的Objective c 使用AFNetworking从中获取JSON null,objective-c,ios,nsurlconnection,afnetworking,Objective C,Ios,Nsurlconnection,Afnetworking,我写的两个代码都是一样的 使用NSURLConnection和 使用AFN网络 我的NSURLConnection代码工作正常,但AFNetworking代码不工作。问题是我从我的NSURLConnection代码中得到了JSON响应,但我没有从AFNetworking代码中得到JSON响应,但我得到的响应代码是200。我尝试了所有可能的组合,但仍然没有运气。我不知道是什么问题。我想继续使用AFNetworking,请有人帮帮我。谢谢你,阿米特 NSURL连接: - (IBAction)conn
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]; }
AFN网络:
- (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
library接收到纯JSON,您将收到相应的对象
编辑
我现在明白你的问题了。您希望AFNetworking
对url表单编码一半的参数,但也对另一部分进行JSON编码。使用AFNetworking
时,必须将整个参数对象编码为一个对象或另一个对象。我建议您更改为100%JSON,方法是保留您创建的homePostParams并使用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表单编码,那就更好了。我不明白为什么要将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
library接收到纯JSON,您将收到相应的对象
编辑
我现在明白你的问题了。您希望AFNetworking
对url表单编码一半的参数,但也对另一部分进行JSON编码。使用AFNetworking
时,必须将整个参数对象编码为一个对象或另一个对象。我建议您更改为100%JSON,方法是保留您创建的homePostParams并使用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实例): 这将为您设置适当的accept标头 对于操作(所有操作都需要json响应),我会执行以下操作:
AFHTTPRequestOperation *httpOperation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseJSONObject) { // some logic}
failure:^(AFHTTPRequestOperation *operation, NSError *error) { // some logic }];
我使用AFNetworking库,但您似乎缺少我所做的一些步骤。我通常是AFHTTPClient的子类。无论是否为子类,请确保调用AFHTTPClient的registerHTTPOperationClass方法 例如,在初始化过程中,在我的AFHTTPClient子类中,我会(其中self是AFHTTPClient实例): 这将为您设置适当的accept标头 对于操作(所有操作都需要json响应),我会执行以下操作:
AFHTTPRequestOperation *httpOperation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseJSONObject) { // some logic}
failure:^(AFHTTPRequestOperation *operation, NSError *error) { // some logic }];
谢谢您的回复,但运气不好。@AmitVyawahare请查看我的编辑。您是否希望使用原始JSON,但得到的是对象?@AmitVyawahare我现在明白了为什么要使用表单编码,我建议您使用100%JSON而不是表单编码。问题在于AFNetworking要么将整个HomePostParam编码为JSON,要么不编码。您希望它是半形式编码和半JSON编码。因此,如果您希望服务器接收任何JSON,那么它必须是100%JSON,或者您必须使用与上述非常类似的编码方案,但这与
AFNetworking
的目的大相径庭,我在编辑时遇到了错误。由于Error Domain=com.alamofire.networking.Error Code=-1016“预期的内容类型{(“text/json”、“application/json”、“text/javascript”)},AFJSONRequestOperation
可能需要这些内容类型之一。或者您可以尝试将内容类型设置为url表单编码。使用类似于[httpClient setParameterEncoding:AFFormURLParameterEncoding]
。这可能有效,但如果您使用的是AFJSONRequestOperation
,您非常希望发送和接收JSON。感谢您的响应,但运气不佳。@AmitVyawahare查看我的编辑。您希望原始JSON,但得到的是对象吗?@AmitVyawahare我现在明白您为什么要进行表单编码了,梅我建议您使用100%的JSON来代替表单编码