Cocoa使用nsurlrequest发布数据

时间:2014-05-07 12:20:04

标签: php ios web-services cocoa

我已成功为我的iphone应用程序制作了PHP REST Web服务。下面是带有url请求的代码,目前正在运行。我的问题是:

如何将此转换为发送带有请求的用户名,以便我可以获取单个用户而不仅仅是所有用户?

我想我需要做一个POST或其他什么。我已经尝试了几个小时,但无法得到任何工作。如何从PHP Web服务中获取POST数据?我认为它不仅仅像_POST一样简单吗?

我完全迷失在这个问题上,感谢任何帮助。

User *newUser = [[User alloc] init];

    NSURL *url = [NSURL URLWithString:@"http://photoapp.steffenamby.dk/REST/userbyusername"];


    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest
                                          returningResponse:&response
                                                      error:&error];


    if(data.length > 0 && error == nil){
        NSDictionary *userdata = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

        [newUser setUserid:[[userdata objectForKey:@"id"] intValue]];
        [newUser setUsername:username];
        [newUser setAge: [[userdata objectForKey:@"age"] intValue]];
        [newUser setFirstname:[userdata objectForKey:@"firstname"]];
        [newUser setLastname:[userdata objectForKey:@"lastname"]];
        [newUser setEmail:[userdata objectForKey:@"email"]];

    }

2 个答案:

答案 0 :(得分:1)

您不需要使用POST请求将参数传递给Web服务器。在您的示例中,最简单的更改是将基于URL的查询参数添加到现有URL:

http://photoapp.steffenamby.dk/REST/userbyname?user=Teilmann

然后在服务器上的PHP代码中,您将查找参数值$ _GET [' user'],在上面的GET URL请求的情况下,将是" Teilmann&# 34。

答案 1 :(得分:0)

我似乎已经弄明白了。

在网络服务中,它就像普通的$ _POST ['用户名']一样简单,以获取价值!

在客户端我将请求更改为:

   -(User *) getUserDetailsByUsername:(NSString *)username{
        User *newUser = [[User alloc] init];

        if([username length] > 0){

            NSURL *aUrl = [NSURL URLWithString:@"http://example.com/userbyusername"];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:60.0];

            [request setHTTPMethod:@"POST"];
            NSString *postString = [NSString stringWithFormat:@"username=%@", username];
            [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

            NSURLResponse *response = nil;
            NSError *error = nil;
            NSData *data = [NSURLConnection sendSynchronousRequest:request
                                                 returningResponse:&response
                                                             error:&error];

            if(data.length > 0 && error == nil){
                NSDictionary *userdata = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

                [newUser setUserid:[[userdata objectForKey:@"id"] intValue]];


[newUser setUsername:[userdata objectForKey:@"username"]];
                [newUser setAge: [[userdata objectForKey:@"age"] intValue]];
                [newUser setFirstname:[userdata objectForKey:@"firstname"]];
                [newUser setLastname:[userdata objectForKey:@"lastname"]];
                [newUser setEmail:[userdata objectForKey:@"email"]];

            }

        }

        return newUser;
    }