1.ASIHTTPRequest一个简短的引论

github下载链接https://github.com/pokeb/asi-http-request

2.ASIHTTPRequest 对GET和POST请求简单封装

+(void)requestWithASIURL:(NSString *)urlString parmas:(NSMutableDictionary *)params httpMethod:(NSString *)method completeBlock:(RequestFinishBlock)block{
//处理GET请求
if ([[method uppercaseString] isEqualToString:@"GET"]) {
NSArray *keys=[params allKeys];
for (int i=0; i<keys.count; i++) {
NSString *key=[keys objectAtIndex:i];
NSString *values=[params valueForKey:key];
urlString= [urlString stringByAppendingFormat:@"&%@=%@",key,values];
} }
NSURL *url=[NSURL URLWithString:urlString];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:method];
[request setTimeOutSeconds:10];
//处理POST请求
if ([[method uppercaseString] isEqualToString:@"POST"]) {
NSArray *keys=[params allKeys];
for (int i=0; i<keys.count; i++) {
NSString *key=[keys objectAtIndex:i];
NSString *value=[params objectForKey:key];
if ([value isKindOfClass:[UIImage class]]) {
// NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test4" ofType:@"gif"];
// NSData* data=[NSData dataWithContentsOfFile:filePath];
NSData *imageData=UIImageJPEGRepresentation(value, 1.0);
[request addData:imageData forKey:key];
}
[request setPostValue:value forKey:key]; }
} [request setCompletionBlock:^{
NSData *data=[request responseData];
NSJSONSerialization *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
block(json);
}];
[request startAsynchronous]; }

3.调用格式

    NSString* urlString=@"http://192.168.1.101:8080/PengFu/jokController/getPhoneJok";
NSMutableDictionary *params=[NSMutableDictionary dictionaryWithObject:@"1 " forKey:@"rows"];
UIImage *image=[UIImage imageNamed:@"test3.gif"];
[params setObject:image forKey:@"pic"];
[params setObject:@"test gif image upload" forKey:@"status"]; [DataService requestWithASIURL:urlString1 parmas:params httpMethod:@"POST" completeBlock:^(id result) {
NSLog(@"%@",result);
}];

4.注意事项

下载代码编译报错请先导入网络请求所需的5个库例如以下



假设使用ARC自己主动内存管理。请在源代码编译后面加-fno-objc-arc,例如以下图


版权声明:本文博主原创文章,博客,未经同意不得转载。

最新文章

  1. Oracle11g的最佳灵活体系结构OFA
  2. LCD内核自带驱动分析
  3. Python3基础 把一个列表中内容给另外一个列表,形成两个独立的列表
  4. 【转】R语言笔记--颜色的使用
  5. PHP读某一个目录下所有文件和文件夹
  6. ubuntu 更新重启后 登录后 无法进入图形界面
  7. 使用SignalR实现比特币价格实时刷新
  8. 【beta】阶段 第六次 Scrum Meeting
  9. Android开发:Android Studio开发环境配置
  10. @Transactional spring 配置事务 注意事项
  11. 涂抹mysql笔记-数据备份和恢复
  12. PHP 类名::class含义
  13. MySQL 出现 Host is blocked because of many connection errors; unblock with &#39;mysqladmin flush-hosts&#39;
  14. VS之解决方案文件夹
  15. Docker命令之 build
  16. WinRT 中后台任务类的声明
  17. 左萧龙(LZ)个人博客
  18. JAVA 比较两个日期相差的天数
  19. 自动make工具--autotools
  20. mongo: 删

热门文章

  1. 开源工具DbUtils的使用(数据库的增删改查)
  2. 端口映射工具 redir/socat/xinetd - 运维技术 - 开源中国社区
  3. Hibernate学习之createSQLQuery与createQuery的区别及使用
  4. 百度词典搜索_dress code
  5. hdu 1542 Atlantis(段树&amp;amp;扫描线&amp;amp;面积和)
  6. VLine2.0——仿阿里巴巴VIPABC真人视频在线教育(基于Flash支持一对多多对多Web在线视频)
  7. zabbix监控nginx连接状态(转)
  8. hdu4578(线段树)
  9. 在内网架设一个可供外网登录的ftpserver
  10. Win7+ubuntu kylin+CentOS 6.5三系统安装图文教程