0

我写了两个代码都一样

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

我的NSURLConnection代码工作正常,但AFNetworking代码不行。问题是我从我的NSURLConnection代码中得到 JSON 响应,但我没有JSONAFNetworking代码中得到响应,但我得到的响应代码为 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]; }
4

2 回答 2

2

我不明白您为什么要将 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,你会收到相应的对象。

编辑

我现在看到你的问题了。您期望AFNetworkingurl 形式对一半参数进行编码,但也对另一部分进行 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 表单编码会更好

于 2012-06-21T19:24:43.593 回答
2

我使用 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 }];
于 2012-06-21T20:59:12.023 回答