gpt4 book ai didi

ios - responseObject 不是 JSON,而是 AFHTTPRequestOperationManager 的 NSInLineData

转载 作者:可可西里 更新时间:2023-11-01 03:29:57 28 4
gpt4 key购买 nike

下面的代码是通过一个operationQueue提交图片。请求都被一个一个正确触发,服务器响应包含客户端需要获取的图像文件名。问题是成功/失败 block 的 reponseObject 不是预期的已解析 JSON,而是调试器中显示的 NSInLineData 类型。现在我怀疑从 NSMutableURLRequest 构造操作的代码导致了这个问题。请帮忙。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST"
URLString:podURLString parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

NSError *error;
BOOL success =[formData appendPartWithFileURL:imgURL name:@"images" fileName:img.path
mimeType:@"image/jpg" error:nil];
if (!success)
NSLog(@"appendPartWithFileURL error: %@", error);} error:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Image Success: %@", [responseObject description]);

NSString *imagePath = [response objectForKey:@"imageFileName"];

[self.delegate networkManager:self didSubmitDeliveryImageForImageID:imagePath];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image Error: %@", error);
NSLog(@"image error: %@", [operation.responseObject description]);

NSString *imageFilePath = [operation.responseObject objectForKey:@"imageFileName"];
[self.delegate networkManager:self didFailSubmitDeliveryImageForImageID:imageFilePath];

}];
[manager.operationQueue addOperation:operation];

最佳答案

当您收到 NSInLineData 响应时。现在可以走了。如果NSDictionary支持json格式,你可以在下面写一行代码来获取NSDictionary。

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObjec options:0 error:nil];

关于ios - responseObject 不是 JSON,而是 AFHTTPRequestOperationManager 的 NSInLineData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27210350/

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