gpt4 book ai didi

ios - AFNetworking http 客户端不发送 JSON 参数

转载 作者:行者123 更新时间:2023-11-29 13:31:43 25 4
gpt4 key购买 nike

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

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

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

根据 AFNetworking FAQ

If you're using AFHTTPClient, set the parameterEncoding property to AFJSONParameterEncoding. Any method on that HTTP client with a parameters argument will now encode the passed object into a JSON string and set the HTTP body and Content-Type header appropriately.

我已在此处完成此操作,但服务器似乎无法识别内容 header 。有人知道潜在的解决方案吗?

方法如下:

- (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");
}

最佳答案

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

没有关于 AFNetworking 的问题。

关于ios - AFNetworking http 客户端不发送 JSON 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11694656/

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