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条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集