標籤:
#import "AFNetworking.h"//主要用於網路要求方法#import "UIKit+AFNetworking.h"//裡面有非同步載入圖片的方法
-(void)obtainData{ // 啟動系統風火輪 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //前面寫伺服器給的網域名稱,後面拼接上需要提交的參數,假如參數是key=1 NSString *domainStr = @"http://192.168.1.69/xffcol/index.php/Api/key/1/"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //以get的形式提交,只需要將上面的請求地址給GET做參數就可以 [manager GET:domainStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // 隱藏系統風火輪 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; //json解析 NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"---擷取到的json格式的字典--%@",resultDic); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 解析失敗隱藏系統風火輪(可以列印error.userInfo查看錯誤資訊) [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }];}
-(void)obtainData{ // 啟動系統風火輪 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //伺服器給的網域名稱 NSString *domainStr = @"http://192.168.1.69/xffcol/index.php/Api/"; //假如需要提交給伺服器的參數是key=1,class_id=100 //建立一個可變字典 NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary]; //往字典裡面添加需要提交的參數 [parametersDic setObject:@"1" forKey:@"key"]; [parametersDic setObject:@"100" forKey:@"class_id"]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //以post的形式提交,POST的參數就是上面的網域名稱,parameters的參數是一個字典類型,將上面的字典作為它的參數 [manager POST:domainStr parameters:parametersDic success:^(AFHTTPRequestOperation *operation, id responseObject) { // 隱藏系統風火輪 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; //json解析 NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"---擷取到的json格式的字典--%@",resultDic); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 解析失敗隱藏系統風火輪(可以列印error.userInfo查看錯誤資訊) [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }];}
-(void)uploadPictures{ //網域名稱 NSString *domainStr = @"http://192.168.1.69/xffcol/index.php/Api/"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //如果還需要上傳其他的參數,參考上面的POST請求,建立一個可變字典,存入需要提交的參數內容,作為parameters的參數提交 [manager POST:domainStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //_imageArray就是圖片數組,我的_imageArray裡面存的都是圖片的data,下面可以直接取出來使用,如果存的是image,將image轉換data的方法如下:NSData *imageData = UIImageJPEGRepresentation(image, 0.7); if (_imageArray.count > 0 ){ for(int i = 0;i < _imageArray.count;i ++){ NSData *data=_imageArray[i]; //上傳的參數名 NSString *name = [NSString stringWithFormat:@"%d",i]; //上傳的filename NSString *fileName = [NSString stringWithFormat:@"%@.jpg",name]; [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpeg"]; } } }success:^(AFHTTPRequestOperation *operation, id responseObject) { //關閉系統風火輪 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; //json解析 NSDictionary * resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"---resultDic--%@",resultDic); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 解析失敗 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }];}
[leftView setImageWithURL:[NSURL URLWithString:@"http://pic25.nipic.com/20121205/5955207_182136452000_2.jpg"] placeholderImage:@"這裡是網狀圖片還沒載入出來的時候,顯示的預設圖片"];
iOS開發 之 AFNetworking的基本使用