HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 问题从AFNetworking 1.3迁移到AFNetworking 2.0大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将项目从AFNetworking 1.3迁移到AFNetworking 2.0.

在AFNetworking 1.3项目中,我有以下代码

- (void) downloadJson:(id)sender
{

    NSURLrequest *request = [NSURLrequest requestWithURL:[NSURL URLWithString:@"http://myServer/api/call?param1=String1&param2=String2"]];

    AFJSONrequestOperation *operation = [AFJSONrequestOperation JSONrequestOperationWithrequest:request success:^(NSURLrequest *request,NShttpURLResponse *response,id JSON) {

        // handle success

    } failure:^(NSURLrequest *request,NSError *error,id JSON) {
        NSLog(@"%ld",(long)[response statusCode]);

        NSDictionary *data = JSON;
        NSString *errorMsg = [data objectForKey:@"descriptiveError@R_@R_197_11248@_8798@ge"];
        // handle failure

    }];

    [operation start];

}

当客户端发送格式不正确或参数错误的网址时,服务器会发回400错误并包含JSON,其中包含我在故障块中读取的“descriptiveError@R_@R_197_11248@_8798@ge”.我使用这个“descriptiveError@R_@R_197_11248@_8798@ge”来确定url的错误,并在适当的时候向用户发送消息.

AFNetworking 2.0项目的代码如下所示:

- (void)downloadJson:(id)sender
{
 NSURLrequest *request = [NSURLrequest requestWithURL:[NSURL URLWithString:@"http://myServer/api/call?param1=String1&param2=String2"]];

    AFhttprequestOperation *operation = [[AFhttprequestOperation alloc] initWithrequest:request];

    operation.responseserializer = [AFJSONResponseserializer serializer];

    [operation setCompletionBlockWithsuccess:^(AFhttprequestOperation *operation,id responSEObject) {

        // handle success

    } failure:^(AFhttprequestOperation *operation,NSError *error) {

        // any way to get the JSON on a 400 error?

    }];

    [operation start];
}

在AFNetworking 2.0项目中,我没有看到任何方法让JSON读取服务器发送的“descriptiveError@R_@R_197_11248@_8798@ge”.我可以在操作中从NShttpURLResponse获取响应头,但就我而言,也许我错过了一些东西.

有没有办法在失败块中获取JSON?如果没有,任何人都可以提出更好的方法吗?

在此先感谢您对此问题的任何帮助.

解决方法

我想你可以尝试访问传递的操作参数的responseData属性你的失败块.

不确定它是否包含服务器发回的JSON数据,但所有信息都应该存在.

希望能帮助到你.

大佬总结

以上是大佬教程为你收集整理的ios – 问题从AFNetworking 1.3迁移到AFNetworking 2.0全部内容,希望文章能够帮你解决ios – 问题从AFNetworking 1.3迁移到AFNetworking 2.0所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。