1. AFNetwork是一个轻量级的网络请求api类库。是以NSURLConnection, NSOperation和其他方法为基础的。
  2. 下面这个例子是用来处理json请求的:
  3. NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
  4. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  5. AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  6. NSLog(@"App.net Global Stream: %@", JSON);
  7. } failure:nil];
  8. [operation start];
  9. 使用方法:
  10. 1.下载AFNetwork,点击下载
  11. 2.将文件夹名称为AFNetworking拖入到你的工程项目即可
  12. 常见问题:
  13. 1. AFNetworking作用都有哪些?
  14. NSURLConnection提供了+sendAsynchronousRequest:queue:completionHandler:和+sendAsynchronousRequest:queue:completionHandler: ,但是AFNetworking提供了更好的功能
  15. *AFURLConnectionOperation和它的子类继承NSOperation的,允许请求被取消,暂停/恢复和由NSOperationQueue进行管理。
  16. *AFURLConnectionOperation也可以让你轻松得完成上传和下载,处理验证,监控上传和下载进度,控制的缓存。
  17. *AFHTTPRequestOperation和它得子类可以基于http状态和内容列下来区分是否成功请求了
  18. *AFNetworking可以将远程媒体数据类型(NSData)转化为可用的格式,比如如JSON,XML,图像和plist。
  19. *AFHTTPClient提供了一个方便的网络交互接口,包括默认头,身份验证,是否连接到网络,批量处理操作,查询字符串参数序列化,已经多种表单请求
  20. *的UIImageView+ AFNetworking增加了一个方便的方法来异步加载图像。
  21. 2. AFNetworking是否支持缓存?
  22. 可以,NSURLCache及其子类提供了很多高级接口用于处理缓存
  23. 如果你想将缓存存储再磁盘,推荐使用SDURLCache
  24. 3.如何使用AFNetworking上传一个文件?
  25. NSData *imageData = UIImagePNGRepresentation(image);
  26. NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id  formData) {
  27. [formData appendPartWithFileData:imageData mimeType:@"image/png" name:@"avatar"];
  28. }];
  29. 4.如何使用AFNetworking下载一个文件?
  30. 先创建一个AFURLConnectionOperation对象,然后再使用它的属性outputStream进行处理
  31. operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO];
  32. 5.如何解决:SystemConfiguration framework not found in project
  33. 请导入:
  34. #import
  35. #import
  36. 6.当应用程序退出时,如何保持持续的请求?
  37. AFURLConnectionOperation有一个叫setShouldExecuteAsBackgroundTaskWithExpirationHandler:的方法用于处理在应用程序进入后台后,进行持续的请求
  38. [self setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
  39. }];
  40. 一些实例:
  41. 1.XML 请求
  42. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792@N01&format=rest"]];
  43. AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
  44. XMLParser.delegate = self;
  45. [XMLParser parse];
  46. } failure:nil];
  47. [operation start];
  48. 2.图片请求:
  49. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
  50. [imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
  51. 3.图片上传处理,监测上传状态:
  52. didiwei  17:28:57
  53. NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
  54. AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
  55. NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
  56. NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id formData) {
  57. [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
  58. }];
  59. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  60. [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
  61. NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
  62. }];
  63. [operation start];
  64. 4.在线流媒体请求
  65. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];
  66. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  67. operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];
  68. operation.outputStream = [NSOutputStream outputStreamToMemory];
  69. [operation start];
  

From:http://www.coneboy.com/?p=716

这几天在评论页面的时候发现get请求后加入2个以上的汉字会出现BadURL提示,断点调试发现是多中文字符惹的祸,所以在URL使用以前先转码成UTF-8即可
这里顺便说明下自己使用AFNetworking 的常用方法。

目前我只是用到
#import “AFHTTPClient.h”;
#import “AFHTTPRequestOperation.h”
这两个类

发起一个请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 NSString *URLTmp = @”http://www.coneboy.com”;
NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码成UTF-8  否则可能会出现错误
URLTmp = URLTmp1;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: URLTmp]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", operation.responseString);

NSString *requestTmp = [NSString stringWithString:operation.responseString];
NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
//系统自带JSON解析
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@”Failure: %@”, error);
[SVProgressHUD dismissWithError:@"提交失败,请重试"];
}];
[operation start];
}

AFNetworking 下载图片
AFNETWorking 下载图片是异步并且可缓存到cache里面 非常好用不会阻塞主线程。用过的都说好!而且使用也简单!
#import “UIImageView+AFNetworking.h”

1
[imageView setImageWithURL:[NSURL URLWithString:URL]];

AFNetworking 使用总结 (用法+JSON解析的更多相关文章

  1. AFNetworking 使用总结 (用法+JSON解析)

    « AFNetworking 图片的本地缓存问题 Get application bundle seed ID in iOS » AFNetworking 使用总结 (用法+JSON解析)    Fr ...

  2. IOS-网络(HTTP请求、同步请求、异步请求、JSON解析数据)

    // // ViewController.m // IOS_0129_HTTP请求 // // Created by ma c on 16/1/29. // Copyright © 2016年 博文科 ...

  3. Linux json解析jq

    jq是一个linux下很方便的json解析器,平时看json文件都是一行混在一起的,非常不适合阅读. https://stedolan.github.io/jq/download/ 直接到官网上下载, ...

  4. 一起写一个JSON解析器

    [本篇博文会介绍JSON解析的原理与实现,并一步一步写出来一个简单但实用的JSON解析器,项目地址:SimpleJSON.希望通过这篇博文,能让我们以后与JSON打交道时更加得心应手.由于个人水平有限 ...

  5. ace布置小作业: 制作一个简单的电话号码归属地查询软件:JSON解析和Volly发送get请求

    大概就这个样子 用到JSON解析和Volly发送Get请求两个知识点 关于Volly的用法请看我的这篇: http://www.cnblogs.com/AceIsSunshineRain/p/5177 ...

  6. Android JSON 解析库的使用 - Gson 和 fast-json

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...

  7. ios 网络数据下载和JSON解析

    ios 网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍ios中如何利用NSURLConnection从网络上下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载 ...

  8. perl6 JSON::Fast模块json解析的使用

    关于JSON: JSON (JavaScript Object Notation)是一种轻量级的数据交换格式,语法简单,各种语言都有相应的库或者模块支持. 因为JSON非常小巧,解析起来又非常简单,我 ...

  9. jquery.cookie用法详细解析,封装的操作cookie的库有jquery.cookie.js

    jquery.cookie用法详细解析 需要注意存入cookie前,对数据进行序列化, 得到后在反序列化: 熟练运用:JSON.stringify();和JSON.parse(): 通常分为如下几个步 ...

随机推荐

  1. Java 类型转换(int->String)

    1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...

  2. oracle service name connect

    oracle service name connect dest_ip=100.100.100.100 dest_port= dest_dbname=server_name connect=" ...

  3. 1.centos7 安装zookeeper

    1.安装jdk 1)查找jdk包: yum search java|grep jdk 2)安装: yum install -y java-1.8.0-openjdk.x86_64 2. 安装ZooKe ...

  4. windows远程xshell文件上传下载:

    安装:yum install -y lrzsz 上传:rz下载:sz 文件名

  5. 下载,安装oracle数据库以及navicat连接数据库

    一.学习时所遇问题: 1.在下载之前以为oracle不是免费的,但是后来才知道oracle对于个人学习时是免费的,可以到官网下载安装.在下载时由于要注册oracle官网,所以尝试了好几遍,才成功下载o ...

  6. 在本地安装oracle-maven库

    mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging= ...

  7. 分配一维动态数组or 二维动态数组的方法以及学习 new 方法or vector

    先来个开胃菜 // 使用new动态分配存储空间 #include<iostream> using std::cout; int main() { // 第1种方式 int *a=new i ...

  8. Silverlight 用DependencyProperty 自定义ImageButton控件 定义属性

    为ImageButton自定义IconSource和Contents属性 xaml代码 <UserControl x:Class="SilverlightCreate.Silverli ...

  9. 错误:子进程 已安装 pre-removal 脚本 返回了错误号 1

    解决办法 sudo rm /var/lib/dpkg/info/<package name>.*

  10. SpringMVC restful风格

    1.Spring对REST的支持 Spring3(这里讨论Spring3.2+)对Spring MVC的一些增强功能为REST提供了良好的支持.Spring对开发REST资源提供以下支持: 操作方式: ...