代码拉取完成,页面将自动刷新
ASIClient是ASIHTTPRequest的扩展,提供简单易用的GET,POST,文件上传,文件下载等网络请求接口。(Block,JSON) 所返回的接口数据,已经做JSON处理,可直接在回调块中,调用 JSON 取得数据,调用stringData 取得 string数据;
+ (ASIHTTPRequest *)GET_Path:(NSString *)path completed:(KKCompletedBlock )completeBlock failed:(KKFailedBlock )failed;
+ (ASIHTTPRequest *)GET_Path:(NSString *)path params:(NSDictionary *)paramsDic completed:(KKCompletedBlock )completeBlock failed:(KKFailedBlock )failed;
+ (ASIHTTPRequest *)POST_Path:(NSString *)path params:(NSDictionary *)paramsDic completed:(KKCompletedBlock )completeBlock failed:(KKFailedBlock )failed;
+ (ASIHTTPRequest *)DownFile_Path:(NSString *)path writeTo:(NSString *)destination fileName:(NSString *)name setProgress:(KKProgressBlock)progressBlock completed:(ASIBasicBlock)completedBlock failed:(KKFailedBlock )failed;
+ (ASIHTTPRequest *)UploadFile_Path:(NSString *)path file:(NSString *)filePath forKey:(NSString *)fileKey params:(NSDictionary *)params SetProgress:(KKProgressBlock )progressBlock completed:(KKCompletedBlock )completedBlock failed:(KKFailedBlock )failed;
+ (ASIHTTPRequest *)UploadData_Path:(NSString *)path fileData:(NSData *)fData forKey:(NSString *)dataKey params:(NSDictionary *)params SetProgress:(KKProgressBlock )progressBlock completed:(KKCompletedBlock )completedBlock failed:(KKFailedBlock )failed;
+ (ASIHTTPRequest *)ResumeDown_Path:(NSString *)path writeTo:(NSString *)destinationPath tempPath:(NSString *)tempPath fileName:(NSString *)name setProgress:(KKProgressBlock )progressBlock completed:(ASIBasicBlock )completedBlock failed:(KKFailedBlock )failed;
###演示代码
#pragma mark ASI ASIClient Test
//GET请求,无参数
- (void)test_GET_NO_PARAM
{
[ASIClient GET_Path:@"movie/top250" completed:^(id JSON, NSString *stringData) {
NSLog(@"%@",JSON);
} failed:^(NSError *error) {
NSLog(@"%@",error);
}];
}
//GET请求,有参数
- (void)test_GET_WITH_PARAM
{
[ASIClient GET_Path:@"movie/top250" params:@{@"count": @"20",@"start":@"5"} completed:^(id JSON, NSString *stringData) {
NSLog(@"%@",JSON);
} failed:^(NSError *error) {
NSLog(@"%@",error);
}];
}
//POST请求
- (void)test_POST_REQUEST
{
[ASIClient POST_Path:@"movie/top250" params:@{@"start": @"30"} completed:^(id JSON, NSString *stringData) {
NSLog(@"%@",JSON);
} failed:^(NSError *error) {
NSLog(@"%@",error);
}];
}
//文件下载
- (void)test_ASIClient_DownFile
{
[ASIClient DownFile_Path:kMusic_DOWN writeTo:@"/Users/cocoajin/Desktop/" fileName:@"downMusic.mp3" setProgress:^(float progress) {
[SVProgressHUD showProgress:progress status:@"正在下载"];
} completed:^{
[SVProgressHUD showSuccessWithStatus:@"下载成功"];
} failed:^(NSError *error) {
NSLog(@"下载失败%@",error);
}];
}
//文件上传
- (void)test_ASIClient_FILE_UP
{
NSString *url = @"http://192.168.1.193:82/index.php/Evidence/uploadTest";
[ASIClient UploadFile_Path:url file:@"/Users/user/Desktop/zyzz.mp3" forKey:@"Data" params:nil SetProgress:^(float progress) {
[SVProgressHUD showProgress:progress status:@"正在上传"];
} completed:^(id JSON, NSString *stringData) {
NSLog(@"文件上传 completed");
NSLog(@"%@",stringData);
[SVProgressHUD showSuccessWithStatus:@"文件上传成功"];
} failed:^(NSError *error) {
NSLog(@"%@",error);
}];
}
//Data数据上传
- (void)test_ASIClient_DATA_UP
{
NSString *url = @"http://192.168.1.193:82/index.php/Evidence/uploadTest";
NSString *fileStr = @"/Users/user/Desktop/zyzz.mp3";
NSData *theData = [NSData dataWithContentsOfFile:fileStr];
[ASIClient UploadData_Path:url fileData:theData forKey:@"Data" params:nil SetProgress:^(float progress) {
[SVProgressHUD showProgress:progress status:@"正在上传"];
} completed:^(id JSON, NSString *stringData) {
NSLog(@"文件上传 completed");
NSLog(@"%@",stringData);
[SVProgressHUD showSuccessWithStatus:@"文件上传成功"];
} failed:^(NSError *error) {
NSLog(@"%@",error);
}];
}
//文件上传,有参数
- (void)test_ASIClient_UP_FILE_WITH_PARAMS
{
///Evidence/fileNonRealTimeAdd
NSString *url = @"http://192.168.1.193:82/index.php/Evidence/fileNonRealTimeAdd";
NSString *fileStr = @"/Users/user/Desktop/zyzz.mp3";
NSDictionary *params = @{@"UserId": @"10010",@"SceneId":@"2",@"Name":@"zyzz.mp3",@"Type":@"0"};
[ASIClient UploadFile_Path:url file:fileStr forKey:@"file" params:params SetProgress:^(float progress) {
[SVProgressHUD showProgress:progress status:@"正在上传"];
} completed:^(id JSON, NSString *stringData) {
NSLog(@"文件上传 completed");
NSLog(@"%@",stringData);
[SVProgressHUD showSuccessWithStatus:@"文件上传成功"];
} failed:^(NSError *error) {
NSLog(@"%@",error);
}];
}
调用接口:
/**
* 文件下载,支持断点续传功能;
*
* @param path 接口路径,不能为空;
* @param destinationPath 下载文件要保存的路径,不能为空;
* @param tempPath 临时文件保存的路径,不能为空;
* @param name 下载保存的文件名,不能为空;
* @param progressBlock 下载文件的Progress块,返回 float progress,在此跟踪下载进度;
* @param completedBlock 下载完成回调块,无回返值;
* @param failed 下载失败回调块,返回 NSError *error;
*
* @return 返回ASIHTTPRequest的指针,可用于 NSOperationQueue操作
*/
+ (ASIHTTPRequest *)ResumeDown_Path:(NSString *)path writeTo:(NSString *)destinationPath tempPath:(NSString *)tempPath fileName:(NSString *)name setProgress:(KKProgressBlock )progressBlock completed:(ASIBasicBlock )completedBlock failed:(KKFailedBlock )failed;
demo 测试
- (void)test_ASIClient_RESUME_DOWN_FILE
{
NSString *resumePath = k360zip;
NSString *destionPath = @"/Users/user/Desktop";
NSString *fileName = @"360zip.exe";
NSString *tempPath = @"/Users/user/Desktop/";
resumeASIClientRequest = [ASIClient ResumeDown_Path:resumePath writeTo:destionPath tempPath:tempPath fileName:fileName setProgress:^(float progress) {
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showProgress:progress status:@"正在下载"];
});
} completed:^{
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showSuccessWithStatus:@"下载成功!"];
});
NSLog(@"complete");
} failed:^(NSError *error) {
NSLog(@"下载失败%@",error);
}];
//[asiNetQueue addOperation:resumeASIClientRequest];
//[asiNetQueue go];
//[resumeASIClientRequest startAsynchronous];
}
Email dev.keke@gmail.com
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。