zoukankan      html  css  js  c++  java
  • MyAFNetWorking MyJsonModel ASIFormData MySimpleAFNetWorking

    #import <UIKit/UIKit.h>
    #import "AFNetworking.h"
    #import "SuccessViewController.h"
    @interface RootViewController : UIViewController
    {
        //声明加载数据的AFNetWork成员变量
        AFHTTPRequestOperationManager *_manager;
        
        
        SuccessViewController *_successController;
    }
    - (IBAction)pressReg:(id)sender;
    - (IBAction)pressLogin:(id)sender;
    @property (weak, nonatomic) IBOutlet UITextField *nameTextField;
    @property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
    @property (weak, nonatomic) IBOutlet UITextField *emailTextField;
    
    @end
    
    #import "RootViewController.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)pressReg:(id)sender {
        
        //实例化
        _manager=[AFHTTPRequestOperationManager manager];
        //AF 不但具有加载数据功能,而且可以自动解析数据,但是解析数据时要求接口格式很严个,所以关闭自动解析数据功能
        //AF 框架默认解析的数据格式为JSON,如果是XML格式,需要设置解析类型
        _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        
        
        //第一个参数为网址
        //第二个参数为服务器需要接收参数
        NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:self.nameTextField.text,@"username",self.pwdTextField.text,@"password",self.emailTextField.text,@"email", nil];
        
        //将参数封装到字典,通过携带字典的网址发送请求,成为POST请求
        [_manager GET:@"http://10.0.8.8/sns/my/register.php" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //当相应成功时
            //NSLog(@"%@",responseObject);
            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",dic);
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //当相应失败时
            NSLog(@"%@",error.localizedFailureReason);
            
            
        }];
        
        
        
        
        
        
    }
    
    - (IBAction)pressLogin:(id)sender {
        
        //实例化
        _manager=[AFHTTPRequestOperationManager manager];
        //AF 不但具有加载数据功能,而且可以自动解析数据,但是解析数据时要求接口格式很严个,所以关闭自动解析数据功能
        //AF 框架默认解析的数据格式为JSON,如果是XML格式,需要设置解析类型
        _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        
        //拼接完成网址,通过完整网址发送请求,成为GET请求
        //GET请求的网址大小不能超过1024B
        NSString *strURL=[NSString stringWithFormat:@"http://10.0.8.8/sns/my/login.php?username=%@&password=%@",self.nameTextField.text,self.pwdTextField.text];
        NSLog(@"%@",strURL);
        [_manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //当相应成功时
            //NSLog(@"%@",responseObject);
            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",dic);
            if ([[dic objectForKey:@"code"] isEqualToString:@"login_success"]) {
                //跳转到登录成功页面
                _successController=[[SuccessViewController alloc]initWithNibName:@"SuccessViewController" bundle:nil];
                [self presentViewController:_successController animated:YES completion:^{
                    
                }];
                
            }
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //当相应失败时
            NSLog(@"%@",error.localizedFailureReason);
            
            
        }];
        
        
    }
    @end
    
    
    #import <UIKit/UIKit.h>
    #import "AFNetworking.h"
    @interface SuccessViewController : UIViewController
    {
        AFHTTPRequestOperationManager *_manager;
    }
    - (IBAction)pressBtn:(id)sender;
    
    @end
    
    #import "SuccessViewController.h"
    
    @interface SuccessViewController ()
    
    @end
    
    @implementation SuccessViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)pressBtn:(id)sender {
        _manager=[AFHTTPRequestOperationManager manager];
        _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        //上传图片
        [_manager POST:@"http://10.0.8.8/sns/my/upload_photo.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            //添加准备上传的图片
        
            //获得图片路径
            NSString *strURL=[[NSBundle mainBundle]pathForResource:@"b" ofType:@"png"];
            //将本地图片路径封装成NSURL类型
            NSURL *url=[NSURL fileURLWithPath:strURL];
            
            //上传图片
            [formData appendPartWithFileURL:url name:@"attach" error:nil];
            
            
            
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //相应成功时调用
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",dict);
            
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //相应失败时调用
            NSLog(@"%@",error.localizedFailureReason);
            
            
        }];
        
        
    }
    @end
    #import "ZYKAppDelegate.h"
    #import "Student.h"
    @implementation ZYKAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        
        
        //JsonModel作用为将数据转换成模型
        NSString *str=@"{"name":"zhangsan","age":20,"array":["abc"]}";
        //用数据初始化对象
        Student *stu=[[Student alloc]initWithString:str error:nil];
        NSLog(@"%@",stu);
        NSLog(@"%@",stu.toDictionary);
        NSLog(@"%@",stu.toJSONString);
        
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    #import <UIKit/UIKit.h>
    #import "ASIFormDataRequest.h"
    
    
    @interface ZYKAppDelegate : UIResponder <UIApplicationDelegate,ASIHTTPRequestDelegate>
    {
        ASIFormDataRequest *_request;
    }
    
    @property (strong, nonatomic) UIWindow *window;
    
    @end
    
    #import "ZYKAppDelegate.h"
    
    @implementation ZYKAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        
        //通过POST请求,加载数据
        NSURL *url=[NSURL URLWithString:@"http://10.0.8.8/sns/my/login.php"];
        _request=[[ASIFormDataRequest alloc]initWithURL:url];
        
        //设置请求方式
        [_request setRequestMethod:@"post"];
        //添加参数
        [_request addPostValue:@"AB__AB__ab" forKey:@"username"];
        [_request addPostValue:@"aaa" forKey:@"password"];
        //设置代理回调
        _request.delegate=self;
        //请求数据
        [_request startAsynchronous];
        
        
        
        
        
        
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    - (void)requestFinished:(ASIHTTPRequest *)request
    {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dict);
    }
    - (void)requestFailed:(ASIHTTPRequest *)request
    {
        NSLog(@"请求失败");
    }
    #import <UIKit/UIKit.h>
    #import "AFNetworking.h"
    @interface ZYKAppDelegate : UIResponder <UIApplicationDelegate>
    {
        AFHTTPRequestOperationManager *_manager;
    }
    
    @property (strong, nonatomic) UIWindow *window;
    
    @end
    
    #import "ZYKAppDelegate.h"
    
    @implementation ZYKAppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        
        //通过AFNetWorking 解析网络数据
        _manager=[AFHTTPRequestOperationManager manager];
        //禁止解析
        _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        //加载数据
        [_manager GET:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //解析数据
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",dict);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        
        
        
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
  • 相关阅读:
    【高级内部资料】.NET数据批量写入性能分析 第二篇
    负载均衡原理与实践详解 第五篇 负载均衡时数据包流程详解
    负载均衡原理与实践详解 第三篇 服务器负载均衡的基本概念网络基础
    如何提高Linq查询的性能(上)
    【全面解析DeepZoom 之二】Silverlight2及Deep Zoom环境的搭建
    关于让WPF软件界面支持全球化和本地化
    在WPF中自定义控件(3) CustomControl (上)
    【全面解析DeepZoom 之一】酷!Deep Zoom
    谈谈我理解的WPF团队模型——在UI Designer与Developer之间
    [WPF疑难]在WPF中显示动态GIF
  • 原文地址:https://www.cnblogs.com/y16879w/p/4490854.html
Copyright © 2011-2022 走看看