-(void)testGet:(UIButton*)b
{
/* Request操作管理器单例得使用 */
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
//设置返回值类型,默认类型为Json
manager.responseSerializer= [AFJSONResponseSerializerserializerWithReadingOptions:NSJSONReadingAllowFragments];
/** 开始请求数据, GET,请求URL parameters,请求参数 , 返回值, AFHTTPRequestOperation,请求操作对象 */
AFHTTPRequestOperation * o1 = [manager GET:@"https://api.weibo.com/2/statuses/public_timeline.json"parameters:@{@"access_token":@"2.00evHF2Cm9O2FC6cabf489d8zODP6E",@"count":@200}success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
//设置请求出错时的警告框
[UIAlertView showAlertViewForRequestOperationWithErrorOnCompletion:o1
delegate:self];
//为o1操作单独设置状态栏中的小菊花
// [[[UIActivityIndicatorView alloc]init]setAnimatingWithStateOfOperation:o1];
/** 为所有AFNetWorking发送的请求设置小菊花. */
[[AFNetworkActivityIndicatorManager sharedManager]setEnabled:YES];
[o1 setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long longtotalBytesExpectedToRead) {
//bytesRead,上次读取的数据 //totalBytesRead,目前为止总共读取的数据 //totalBytesExpectedToRead,预测的文件大小
}];
/** 设置下载进度条 */
UIProgressView * progressV1 = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
[progressV1 setFrame:CGRectMake(0, 80, 320, 30)];
[self.view addSubview:progressV1];
//将o1操作的进度交由progressV展示
[progressV1 setProgressWithDownloadProgressOfOperation:o1 animated:YES];
-(void)testPost:(UIButton*)b
{
/*文件上传示例 */
//设置接收响应类型为标准HTTP类型(默认为响应类型为JSON)
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation * o2= [manager POST:@"http://wunantest.sinaapp.com/filetest.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData * data= UIImagePNGRepresentation([UIImage imageNamed:@"icon.png"]);
//将得到的二进制图片拼接到表单中 /** data,指定上传的二进制流;name,服务器端所需参数名;fileName,指定文件名;mimeType,指定文件格式 */
[formData appendPartWithFileData:data name:@"file" fileName:@"111icon.png" mimeType:@"image/png"];
//多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions)
}success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
//设置上传操作的进度
[o2 setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long longtotalBytesExpectedToWrite) {
}];
UIProgressView * progressV2 = [[UIProgressViewalloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
[progressV2 setProgressWithUploadProgressOfOperation:o2 animated:YES];
[progressV2 setFrame:CGRectMake(0, 130, 320, 30)];
[self.view addSubview:progressV2];
}