1

我想向我的服务器发送一个简单的 Json 并通过我的 iPad 应用程序获得 Json 响应。我使用 Restkit 完成所有工作,但 Json 的 POST 有问题。这是我想要的简单 Json 请求:

{"id_partita":"2"}

响应将是这样的:

[{"id":34,"id_dispositivo":"123","id_partita":2,"allenatore":0,"giocatore1":"AAA","giocatore2":"BBB",...}]

这是我在 Xcode 中的代码:

-(void) post
{
    RKURL *baseURL = [RKURL URLWithBaseURLString:@"mysite"];
    RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];

    // Setup object mappings
    RKObjectMapping *formazioneMapping = [RKObjectMapping mappingForClass:[Formazione class]];

    [formazioneMapping mapKeyPath:@"id_formazione"
                                toAttribute:@"id_formazione"];
    [formazioneMapping mapKeyPath:@"id_dispositivo"
                                toAttribute:@"id_dispositivo"];
    [formazioneMapping mapKeyPath:@"id_partita"
                                toAttribute:@"id_partita"];
    [formazioneMapping mapKeyPath:@"formazione"
                                toAttribute:@"formazione"];
    [formazioneMapping mapKeyPath:@"giocatore1"
                                toAttribute:@"giocatore1"];
    [formazioneMapping mapKeyPath:@"giocatore2"
                                toAttribute:@"giocatore2"];
;

    // Register our mappings with the provider
    [objectManager.mappingProvider setMapping:formazioneMapping forKeyPath:@""];

    [objectManager.router routeClass:[Formazione class]
                      toResourcePath:@"/formazione/index.php"
                           forMethod:RKRequestMethodPOST];

    // Set serialization MIME type.
    [RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeFormURLEncoded;

    // Configure a serialization mapping for our Formazione class. We want to send id_partita
    RKObjectMapping *formazionePOSTSerializationMapping = [RKObjectMapping
                                                      mappingForClass:[Formazione class]];
    [formazionePOSTSerializationMapping mapAttributes:@"id_partita", nil];

    // Now register the mapping with the provider
    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:formazionePOSTSerializationMapping forClass: [Formazione class]];

    FormazionePOST *formazionePOST = [[FormazionePOST alloc] init];
    formazionePOST.id_partita = @"2";
    [[RKObjectManager sharedManager] postObject:formazionePOST delegate:self];

}

但是当我尝试执行它时出现此错误:'无法为 HTTP 方法'POST'的'FormazionePOST'类型的对象找到可路由路径'。

我不明白我的错误是什么,我找不到 POST 和 GET a Json 的简单示例。谢谢您的帮助!

4

2 回答 2

0

是不是在您的路由器初始化中您引用了 Formazione 类,但您正在尝试发布 FormazionePost 类?

于 2012-11-01T17:28:48.437 回答
0

我已经使用 AfNetworking 而不是 RestKit 解决了我的问题。他们的文档中有很多关于 JSON 管理的示例。

于 2014-07-21T10:28:08.040 回答