iOS 和 RestKit : How to get a text/html response right?

标签 ios objective-c rest restkit

我已经尝试了几个 StackOverflow 问题,但我找不到正确的答案。我正在使用 Chrome 的 POSTMAN 插件来检查我的 REST 调用,但我无法弄清楚为什么我无法读取响应。在评论中,您将看到我为获得回复所做的所有不同尝试。

NSDictionary* session_params = @{SESSION_USERNAME_KEY:SESSION_USERNAME_VALUE, SESSION_PASSWORD_KEY:SESSION_PASSWORD_VALUE};
NSURL* url = [NSURL URLWithString:SESSION_URL];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
//GET THE **** THING TO INTERPRET A TEXT response

//[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeTextXML];
//[objectManager setAcceptHeaderWithMIMEType:@"text/html"];
//[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeTextXML];
//[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/html"];
//[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];
//[objectManager setRequestSerializationMIMEType:@"text/html"];

//END
NSMutableURLRequest* request = [objectManager requestWithObject:nil method:RKRequestMethodPOST path:SESSION_URL parameters:session_params];


RKObjectRequestOperation* operation = [objectManager
                                       objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation* operation, RKMappingResult* result)
                                       {
                                           NSLog(@"RESULT [%@]", result);
                                       }
                                       failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                           NSLog(@"ERROR [%@]", error);
                                       }];

    [operation start];

我认为最烦人的是我需要的东西都包含在 NSLocalizedRecoverySuggestion 值中。这是我需要的 session key 。

输出:

E restkit.network:RKObjectRequestOperation.m:547 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {( "application/x-www-form-urlencoded", "application/json" )}, got text/html" UserInfo=0x1c52aed0 {NSLocalizedRecoverySuggestion=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJCbG8uUmVnQWxlcnQuQnJva2VyIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdC9CbG8uUmVnQWxlcnQuQVBJL2FwaSIsIm5iZiI6MTM5MjY0MTY2MSwiZXhwIjoxMzkyNjQ1MjYxLCJ1bmlxdWVfbmFtZSI6IkJ1dHRvbnMiLCJyb2xlIjoiUmVnQWxlcnRDb25zdW1lciJ9.JCTMGJRKlOxEtNrcGodpce-tqsRS4zlApNisKQW6iSw, AFNetworkingOperationFailingURLRequestErrorKey=, NSErrorFailingURLKey=http://..., NSLocalizedDescription=Expected content type {( "application/x-www-form-urlencoded", "application/json" )}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=} 2014-02-17 14:54:20.808 AppName[5600:6403] E restkit.network:RKObjectRequestOperation.m:213 POST 'http://...' (200 OK / 0 objects) [request=0.0000s mapping=0.0000s total=0.1925s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {( "application/x-www-form-urlencoded", "application/json" )}, got text/html" UserInfo=0x1c52aed0 {NSLocalizedRecoverySuggestion=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJCbG8uUmVnQWxlcnQuQnJva2VyIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdC9CbG8uUmVnQWxlcnQuQVBJL2FwaSIsIm5iZiI6MTM5MjY0MTY2MSwiZXhwIjoxMzkyNjQ1MjYxLCJ1bmlxdWVfbmFtZSI6IkJ1dHRvbnMiLCJyb2xlIjoiUmVnQWxlcnRDb25zdW1lciJ9.JCTMGJRKlOxEtNrcGodpce-tqsRS4zlApNisKQW6iSw, AFNetworkingOperationFailingURLRequestErrorKey=, NSErrorFailingURLKey=http://..., NSLocalizedDescription=Expected content type {( "application/x-www-form-urlencoded", "application/json" )}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=}



有效的代码
感谢 Wain 为我指明了正确的路径。我对 RestKit 无法处理如此简单的请求感到有点失望,我需要 RestKit 因为这只是调用其他方法的 session token ,但我猜不管用什么:
NSDictionary* session_params = @{SESSION_USERNAME_KEY:SESSION_USERNAME_VALUE, SESSION_PASSWORD_KEY:SESSION_PASSWORD_VALUE};

NSURL* url = [NSURL URLWithString:SESSION_URL];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:SESSION_URL parameters:session_params];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString* response = [operation responseString];
    NSLog(@"response: %@",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@", [operation error]);
}];
[operation start];

最佳答案

这一点:

"Expected content type {( "application/x-www-form-urlencoded", "application/json" )}, got text/html"



告诉你你已经告诉 RestKit 期待 form-urlencodedjson ,但服务器正在返回 html。

您可能想使用 setAcceptHeaderWithMIMEType用 JSON mime 类型告诉服务器你想要什么。但是,在这种情况下,您可能不应该使用 RestKit。

RestKit 用于将任意 JSON/XML 数据映射到您的数据模型中。你只有一把 key 回来了。不需要映射。因此,不要使用 RestKit,而是使用 AFNetworking(您可以完全访问它,因为 RestKit 在内部使用它。

关于iOS 和 RestKit : How to get a text/html response right?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21830144/

相关文章:

ios - 调用 CFRelease(ABAddressBookRef ref) 时崩溃

rest - PowerShell REST内部主体

javascript - 使用 AngularJS 编辑数组中的非字符串对象

ios - AFNetworking 显示不支持的 URL 用于 http get 操作?

java - Apache CXF - 处理操作中丢失的路径参数

ios - 在 swift 中,为什么我必须使用 IBaction 或 IBOutlet 在代码和 UI 之间进行通信?

iphone - 从相机中确定人物

iphone - 如何使部分的 UITableView 页脚不粘

iOS 7 : UINavigationController pulls down UIScrollview

ios - 带模糊的 UITabBar - Storyboard