Вопрос

I have successfully made a PHP REST webservice for my iphone app. Below is the code with the url request, which works at the moment. My question is:

How do I convert this to send a username with the request, so i can fetch a single user and not just all users?

I think I need to do a POST or something. I have tried for hours but cannot get anything to work. And how do I fetch the POST data from my PHP web service? I assume it's not just as simple as _POST?

I am totally lost on this problem, any help is appreciated.

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

I seem to have figured it out myself.

In the web service it was as simple as an ordinary $_POST['username'] to get the value !

On the client i changed the request to this:

   -(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;
    }

Другие советы

You don't need to use a POST request to pass a parameter to a web server. In your example case the simplest change to make would be to add a URL based query parameter to the existing URL:

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

Then in your PHP code on the server you would look for a parameter value $_GET['user'], which in the case of the GET URL request above, would be "Teilmann".

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top