如何在iOS应用中使用网络服务器?样本 [英] How to use web server with iOS app? sample

查看:133
本文介绍了如何在iOS应用中使用网络服务器?样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了类似的问题,并且得到了如何在iOS应用中使用网络服务器?"的答复.但我需要示例,因为获取示例如何工作更简单,例如,您可以帮我提供该示例吗?

I write similar question and I got answere on 'How to use web server with iOS app?' but I need sample because it is more simple for me to have some sample how this works, for example can you help me with that sample:

如果我想/注册新用户,如何在服务器上实施请求,我将以JSON格式接收服务器的应答,并且需要在变量'data'中将其发送给POST?例如,服务器是 http://myserver.com:8080 .

how can I implement request on my server for exhample if I want /register new user and I will receive the server's answere in JSON and I need to send it it POST in variable 'data' ? For exhample server is http://myserver.com:8080.

{ 
 email:
 password:
}  //this is register form but  how named this field?

我将收到这样的答覆:

{
result:[OK|fail]
 message: [error if fail]
 data: [if OK]
}

我需要示例如何发送和接收/注册请求.对不起,我的英语更好:)

I need sample how can I send and receive /register request. Sorry for my english, it comes better :)

更新另外我不明白如何在Objective-C中使用这种形式?

UPDATE Also I can not understand how can I use this forms in Objective-C?

推荐答案

所以,这里有我的寺庙要发送登录请求,我已经实现了AFNetworking(

So, here you have my temple to send Login req, I have implemented AFNetworking(https://github.com/AFNetworking/AFNetworking) and NSJSONSerialization for parsing Json response. It is hard to give you a sample code, but I think if you customize this piece of code everything should work fine.

        - (void)sentLoginReqWithLogin:(NSString *)email andPassword:(NSString *)pass {

            NSDictionary *params = @{@"email" : email,
                                     @"password" : pass
                                     };


            AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST_URL]];
            NSMutableURLRequest *request = [httpClient requestWithMethod:POST
                                                                    path:LOGIN_URL
                                                              parameters:params];


            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
            [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

                NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
                NSDictionary *response = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:operation.responseData options:kNilOptions error:nil];

    //HERE YOU HAVE A RESPONSE (your server answer)
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {         
                NSLog(@"Error: %@", error);      
            }];


            [operation start];

}

这篇关于如何在iOS应用中使用网络服务器?样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆