AFNetwork学习(二)——GET/POST请求

为了学习AFNetwork,自己搭建整理了一下AFNetwork向后台发送请求和后台返回json数据的整个处理过程。利用Struts2搭建了一个后台,提供Action并返回json数据


环境:Xcode6.3

后台:tomcat+myeclipse10.6  框架:struts2


1.新建一个single-view application工程名为IOS_AFNetwork,开发语言选择OC.

2.将AFNetwork库文件加入到工程中,如下图:


3.引入SystemConfiguration.framework、MobileCoreService.framework、Security.framework 。如下图:



4.在main.storyboard中添加两个按钮,发别用来发送get和post请求,并设置对应的响应函数。如下图:



5.发送Get请求

//get请求
- (IBAction)getRuquest:(id)sender {
    //为了测试方便,自己搭建了一个javaweb后台
    NSString *URLString = @"http://localhost:8080/server/login.action";
    //传递参数
    NSDictionary *loginInputValue = @{@"username" : @"1234567890",
                                      @"password" : @"123456"};

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = self.timeInternal;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    self.operation = [manager GET:URLString
                       parameters:loginInputValue
                          success:^(AFHTTPRequestOperation *operation, id responseObject) {
                             //解析后台返回的json串,将json转为字典
                              NSDictionary *responseData = [NSDictionary dictionaryFromJSONData:(NSData *)responseObject];
                              NSLog(@"success:%@",responseData);
                              NSString *name = [responseData objectForKey:@"name"];
                              NSLog(@"name : %@",name);
                          }
                          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                              NSLog(@"error info :%@",[error description]);
                          }];
}

6.发送Post请求

- (IBAction)postRequest:(id)sender {
    
    NSString *URLString = @"http://localhost:8080/server/login.action";
    NSDictionary *loginInputValue = @{@"username" : @"1234567890",
                                      @"password" : @"123456"};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = self.timeInternal;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
   
    self.operation =  [manager POST:URLString
                         parameters:loginInputValue
                            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                            NSDictionary *responseData = [NSDictionary dictionaryFromJSONData:(NSData *)responseObject];
                            NSString *name = [responseData objectForKey:@"name"];
                            NSLog(@"name : %@",name);

                            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error info :%@",[error description]);    }];

}

OC代码下载地址:

http://download.csdn.net/detail/yuj9006/8876937


后台代码下载地址:

http://download.csdn.net/detail/yuj9006/8876999


阅读更多
个人分类: iOS
上一篇xcode armv6 armv7 armv7s arm64
下一篇UIWebView学习——web页面和Native交互
想对作者说点什么? 我来说一句

AFNetwork发送GET/POST请求

2015年07月07日 123KB 下载

Ajax GetPost请求

2018年07月05日 1.81MB 下载

tomcat GET请求POST请求

2012年08月04日 10.95MB 下载

没有更多推荐了,返回首页

关闭
关闭