zoukankan      html  css  js  c++  java
  • ASIHttpRequest加载网络数据和上传数据功能

    使用ASIHttpRequest第三库,需要配置

    二,

    上传数据功能使用ASIFromDataRequest(可以上传二进制和字符串给服务器)

    下面来牛刀小试

    //建立一个工程,导入第三方库,在AppDelegate.h
    #import <UIKit/UIKit.h>
    #import "ASIFormDataRequest.h"
    @interface AppDelegate : UIResponder <UIApplicationDelegate,ASIHTTPRequestDelegate>
    {
        //声明数据请求的成员变量
        ASIFormDataRequest *_request;
    }
    @property (strong, nonatomic) UIWindow *window;
    
    @end
    //在AppDelegate.m里写的代码
    
    //通过POST请求加载数据
    NSURL *url=[NSURL URLWithString:@"http://10.0.8.8/sns/my/login.php"];
    //实例化成对象
    _request=[[ASIFormDataRequest alloc]initWithURL:url];
    //设置请求方式
    [_request setRequestMethod:@"post"];
    //添加参数
    [_request addPostValue:@"aa_Zx" forKey:@"username"];
    [_request addPostValue:@"ffff" forKey:@"password"];
    //设置代理回调
    _request.delegate=self;
    //请求数据
    [_request startAsynchronous];//异步请求,多线程
    #param -mark ASIHttpRequest
    -(void)requestFinished:(ASIHTTPRequest *)request
    {
    NSDictionary *dict=[NSJSONSerialization  JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil];
       NSLog(@"%@",dict);
    
    }
    - (void)requestFailed:(ASIHTTPRequest *)request
    {
        NSLog(@"请求失败");
    }

     三,加载网络数据,ASIHttpRequest(所有格式的数据都能加载)

    -(void)startLoadWebData:(NSString *)strURL
    {
    //实例化ASIHttpRequest对象
    NSURL *url=[NSURL URLWithString:strURL];
    _request=[[ASIHTTPRequest alloc]initWithURL:url];
    _requent.delegate=self;
    //开始加载数据
    [_requenst startAsynchronous];
    }
    #pragma -mark ASIHttpRequestDelegate
    //请求成功时调用该方法
    -(void)requestFinished:(ASIHTTPRequest *)request
    {
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:request.responseData
    options:NSJSONReadingMutableContainers error:nil];
    //代理接收数据
    [self.delegate reciveData:[dict objectForKey:@"application"];
    }
    //请求网络数据失败时调用该方法
    -(void)requestFailed:(ASIHTTPRequest *)request
    {
    NSLog(@"failed");
    }
  • 相关阅读:
    WebClient.UploadData 方法 上载文件数据
    webclient提交并接受返回
    webClient上载下载
    斯特林反演与伯恩赛德引理
    Re0: 从 1 开始的省选前生活
    Windows 8将可能带动触摸屏的发展
    后PC时代的那些事
    关于ASP网页在IIS7.5下访问数失效
    Windows 要终结了?微软要推超级系统?
    未来10年的开放式互联网
  • 原文地址:https://www.cnblogs.com/y16879w/p/4466699.html
Copyright © 2011-2022 走看看