dongya9904 2014-05-07 12:20
浏览 58
已采纳

Cocoa使用nsurlrequest发布数据

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条回答 默认 最新

  • dongrong1856 2014-05-07 13:12
    关注

    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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改