使用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.responseDataoptions:NSJSONReadingMutableContainers error:nil];//代理接收数据[self.delegate reciveData:[dict objectForKey:@"application"];}//请求网络数据失败时调用该方法-(void)requestFailed:(ASIHTTPRequest *)request{NSLog(@"failed");}
新闻热点
疑难解答