【发布时间】:2015-03-07 17:22:10
【问题描述】:
AFNetworking 有问题。
目前我可以使用NSDictionary 以JSON 格式[AFJSONParameterEncoding] 向服务器发送POST 请求,并且它可以正确回复,问题是服务器也回复了JSON 格式的响应,我可以使用以下方法转换为NSString:
[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// responseObject is the server response
问题是我无法以任何其他格式转换响应,除了NSString,就像之前发布的代码一样。这怎么可能?我想将响应转换为 JSON 格式,以便读取精确值,即与键 "isInformative" 关联的值
到目前为止,这是我的代码:
NSDictionary *requestBody = [NSDictionary dictionaryWithObjectsAndKeys:
@"value1", @"key1",
@"value2", @"key2",
nil];
NSDictionary *requestHead = @{
@"RequestHead": requestBody
};
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://XXX.XXX.XXX.XXX:XXXX"]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"/public-mobile-sdk/"
parameters:requestHead];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *requestOperation, id responseObject) {
// Here I can convert the responseObject to NSSTring correctly
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *requestOperation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[requestOperation start];
注意 - 我无法更新捆绑在 Xcode 项目中的 AFNetworking 版本,因为它不是我自己的,所以很遗憾我必须坚持使用 1.X 版本
【问题讨论】:
-
看到这个和其他许多:stackoverflow.com/questions/8606444/…
标签: ios objective-c json post afnetworking