使用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");}
|
新聞熱點
疑難解答