ASIHttpRequest載入網路資料和上傳資料功能,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.responseDataoptions:NSJSONReadingMutableContainers error:nil];//代理接收資料[self.delegate reciveData:[dict objectForKey:@"application"];}//請求網路資料失敗時調用該方法-(void)requestFailed:(ASIHTTPRequest *)request{NSLog(@"failed");}