【问题标题】:AFNetworking http client not sending JSON parametersAFNetworking http 客户端不发送 JSON 参数
【发布时间】:2012-07-27 19:42:07
【问题描述】:

我创建了AFHTTPClient 的子类,并尝试将一些 JSON 参数发送到服务器。

但是服务器以预期的内容类型响应

{(
    "text/json",
    "application/json",
    "text/javascript"
)}, got application/xml

根据AFNetworking FAQ

如果您使用的是 AFHTTPClient,请将 parameterEncoding 属性设置为 AFJSONParameterEncoding。该 HTTP 客户端上的任何带有参数参数的方法现在都会将传递的对象编码为 JSON 字符串,并适当地设置 HTTP 正文和 Content-Type 标头。

我已经在这里完成了,但服务器似乎无法识别内容标题。有人知道潜在的解决方案吗?

方法如下:

- (void)getCompanyDataWithString:(NSString*)companySearchQuery 
      finish:(LBMarkitAPIRequestCompletionBlock)finishBlock
{
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setParameterEncoding:AFJSONParameterEncoding];

    NSDictionary *params = [NSDictionary dictionaryWithObject:
        companySearchQuery forKey:@"input"];
    NSMutableURLRequest *searchQueryRequest = [self requestWithMethod:@"GET"
        path:kMarkitCompanyURL parameters:params];

    AFJSONRequestOperation *searchRequestOperation = [AFJSONRequestOperation 
        JSONRequestOperationWithRequest:searchQueryRequest 
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) 
        {
            NSLog(@"Response: %@", response);
            NSLog(@"JSON: %@",json);
            NSMutableArray *results = [NSMutableArray array];

            NSError *anError = [[NSError alloc] init];
            if ([json objectForKey:@"Message"]) 
            {
                NSString *message = [json objectForKey:@"Message"];
                anError = [[NSError alloc] initWithDomain:message
                                                     code:100 
                                                 userInfo:nil];
            }

            // Need some error handling code here
            for (id item in json) 
            {
                NSString *aName = [item objectForKey:@"Name"];
                NSString *aSymbol = [item objectForKey:@"Symbol"];
                NSString *anExchange = [item objectForKey:@"Exchange"];

                LBCompany *aCompany = [[LBCompany alloc] initWithName:aName 
                    Symbol:aSymbol Exchange:anExchange];
                [results addObject:aCompany];
            }
            // Need to run the passed in block after JSON 
            // Request Operation succeeds

            finishBlock(results,anError);
          }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, 
            NSError *error, id JSON)
        {
            NSLog(@"request failed: %@",[error localizedDescription]);
            NSLog(@"Response: %@",response);
            NSLog(@"JSON: %@",JSON);
        }];

    [searchRequestOperation start];
    NSLog(@"JSON operation started");
}

【问题讨论】:

    标签: ios ios5 nsurlrequest afnetworking


    【解决方案1】:

    问题在于 URL 格式。我没有注意到需要发送查询参数并在 URI 中指定 JSON 输出的 API 实现细节。

    AFNetworking 没有问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2013-12-07
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多