gpt4 book ai didi

objective-c - 发送发布请求并将 map 响应发送给对象

转载 作者:行者123 更新时间:2023-12-01 19:23:49 25 4
gpt4 key购买 nike

我是restKit的新手,有几个问题要问您。一世
无法理解如何使用json / xml将发布请求发送到我的网站
服务,并将收到的回复与我的 class 对应。谁能给
我对此有帮助。我正在使用的代码是这样的:
在我的applicationDelegate中,我正在实例化RKObjectManager
提供基本网址:

RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"https://example.com/services/"]; 

// Request params in Dictionary
NSArray *objects = [NSArray arrayWithObjects: email, password,
nil];
NSArray *keys = [NSArray arrayWithObjects:@"username",
@"password", nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
NSLog(@"Manager: %@", [RKObjectManager
sharedManager].description);
// User object Mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:
[User class]];
[userMapping mapKeyPath:@"userName" toAttribute:@"userName"];
[userMapping mapKeyPath:@"lastName" toAttribute:@"lastName"];
[userMapping mapKeyPath:@"active" toAttribute:@"active"];
[[RKObjectManager sharedManager].mappingProvider
setMapping:userMapping forKeyPath:@"user"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/
login" delegate:self];

当帖子发送到/ login时,服务器应发送回有效的json
然后将该json映射到我的User类。
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects: 
(NSArray*)objects {
RKLogInfo(@"Load collection of Articles: %@", objects);
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:
(NSError *)error
{
NSLog(@"Objecy Loader failed: %@", error);
}

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError
*)error
{
NSLog(@"Request failed");
}

- (void)request:(RKRequest*)request didLoadResponse:
(RKResponse*)response {
if ([request isGET]) {
// Handling GET /foo.xml
if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(@"Retrieved XML: %@", [response bodyAsString]);
}
} else if ([request isPOST]) {
// Handling POST /other.json
if ([response isXML]) {
///NSLog(@"Seng a JSON request:! \n %@", [request
HTTPBodyString]);
NSLog(@"Got a responce! \n %@", [response bodyAsString]);
}
} else if ([request isDELETE]) {
// Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(@"The resource path '%@' was not found.", [request
resourcePath]);
}
}
}

当我执行它时,不会触发objectLoader方法,
Restkit的理解是它们应该在何时被调用
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/login" 
delegate:self];

被执行?任何帮助表示赞赏:)

最佳答案

好吧,您甚至没有将数据发送到服务器。几天前,我写了一段代码来解释how to post a NSDictionary (as JSON) to a server using RestKit

关于objective-c - 发送发布请求并将 map 响应发送给对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8914103/

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