【问题标题】:parse json from post request ios afnetworking从发布请求 ios afnetworking 解析 json
【发布时间】:2014-03-22 07:56:32
【问题描述】:

我正在开发一个 iOS 应用程序(对 iOS 来说非常新),我的注册流程正在工作,我将数据发布到我的服务器......服务器返回 JSON,但我的问题是,我如何将其解析为变量?

它返回的数据是:

 {"token":"67f41f60-9a9a-11e3-8310-11b6baf18c40","userId":13,"stationId":1}

我用来发出帖子请求的代码是:

 [manager POST:@"http://localhost:3000/service/register_user/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error!!!!!: %@", error);
}];

另外,当我发出 post 请求时,这是我在 xcode 中的日志中得到的

 2014-02-20 20:50:39.799 FlashoverResponse[4668:70b] Error!!!!!: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8a5e4f0 {NSErrorFailingURLKey=http://localhost:3000/service/register_user/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a5e200> { URL: http://localhost:3000/service/register_user/ } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 74;
"Content-Type" = "text/html; charset=utf-8";
Date = "Fri, 21 Feb 2014 01:50:39 GMT";
"X-Powered-By" = Express;

} }, NSLocalizedDescription=请求失败:不可接受的内容类型:text/html}

【问题讨论】:

  • 您的服务器需要使用内容类型“application/json”进行响应。任何现代 MVC 或 Web API 框架都会让这变得非常简单。

标签: ios objective-c json afnetworking


【解决方案1】:

您的服务器是否配置为响应 text/json application/json

和/或你设置了这个?

manager.responseSerializer.acceptableContentTypes

它需要 NSSet 的内容类型,此解析器将尝试解析,如果您无法更改服务器以在 json 中响应,您可以将 text/html 添加到该集合中,因此解析器无论如何都会解析它。

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多