1 将afnetworking文件夹导入项目
2 添加类库 security.framework、mobilecoreservices.framework、systemconfiguration.framework
3 在使用的地方 #import "afnetworking.h"
解决编译时警告:
prefix.pch文件中加入
#import <systemconfiguration/systemconfiguration.h>
#import <mobilecoreservices/mobilecoreservices.h>
注:afnetworking使用了arc ,在不使用arc项目中使用时,对afnetworking的所有.m文件添加“-fobjc-arc”
在使用arc项目中,使用“不使用arc”的类库时,对类库的.m文件添加“-fno-objc-arc”
static nsstring*const baseurlstring = @"http://www.raywenderlich.com/downloads/weather_sample/";
// 1 nsstring *weatherurl = [nsstringstringwithformat:@"%@weather.php?format=json",baseurlstring]; nsurl *url = [nsurlurlwithstring:weatherurl]; nsurlrequest *request = [nsurlrequestrequestwithurl:url]; // 2 afjsonrequestoperation *operation = [afjsonrequestoperationjsonrequestoperationwithrequest:request success:^(nsurlrequest*request, nshttpurlresponse *response, id json) { // nsdictionary*dicweather = (nsdictionary *)json; nslog(@"result:%@",dicweather); } failure:^(nsurlrequest*request, nshttpurlresponse *response, nserror *error, id json) { uialertview*alertview = [[uialertview alloc] initwithtitle:@"error retrievingweather" message:[nsstringstringwithformat:@"%@",error] delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alertview show]; }]; // 5 [operation start];
(1)根据基本的url构造除完整的一个url,然后通过这个完整的url获得一个nsurl对象,然后根据这个url获得一个nsurlrequest。
(2)afjsonrequestoperation是一个完整的类,整合了从网络中获取数据并对json进行解析。
(3)当请求成功,则运行成功块。在本例中,把解析出来的天气数据从json变量转换为一个字典(dictionary),并将其存储在字典中。
(4)如果运行出问题了,则运行失败块(failure block),比如网络不可用。如果failure block被调用了,将会通过提示框显示错误信息。
6.afnetworking异步加载图片
[plain] view plaincopy
[list=1](1)#import “uiimageview+afnetworking.h” (2)uiimageview *imageview = [[uiimageview alloc]initwithframe:cgrectmake(40, 80, 40, 40)]; __weak uiimageview *_imageview = imageview; [imageviewsetimagewithurlrequest:[[nsurlrequest alloc] initwithurl:[nsurlurlwithstring:@"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"]] placeholderimage:[uiimage imagenamed:@"placeholder.png"] success:^(nsurlrequest *request,nshttpurlresponse *response, uiimage *image) { _imageview.image = image;
[_imageview setneedsdisplay]; } failure:^(nsurlrequest *request, nshttpurlresponse*response, nserror *error) { ; }]; [self.view addsubview:imageview];
7.get 和post请求
(1).构建一个baseurl,以及一个参数字典,并将这两个变量传给afhttpclient.
(2).将afjsonrequestoperation注册为http的操作, 这样就可以跟之前的示例一样,可以获得解析好的json数据。
(3).做了一个get请求,这个请求有一对block:success和failure。
(4).post请求跟get一样
[list=1]afhttpclient *client= [[afhttpclient alloc] initwithbaseurl:baseurl]; [clientregisterhttpoperationclass:[afjsonrequestoperation class]]; [clientsetdefaultheader:@"accept" value:@"application/json"]; [client postpath:@"weather.php" parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) { self.weather =responseobject; self.title = @"httppost"; [self.tableviewreloaddata]; } failure:^(afhttprequestoperation *operation, nserror*error) { uialertview *av =[[uialertview alloc] initwithtitle:@"error retrieving weather" message:[nsstringstringwithformat:@"%@",error] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [av show]; } ];
[client getpath:@"weather.php" parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) { self.weather =responseobject; self.title = @"http get"; [self.tableviewreloaddata]; } failure:^(afhttprequestoperation *operation, nserror*error) { uialertview *av =[[uialertview alloc] initwithtitle:@"error retrieving weather" message:[nsstringstringwithformat:@"%@",error] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [av show];
} ];
另外,请求方式可以创建一个类继承afhttpclient ,官方的例子就是这样写的。
状态栏设置
在appdelegate里面的 - (bool)application:(uiapplication *)application
didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 方法中添加 [[afnetworkactivityindicatormanager sharedmanager] setenabled:yes];用来给用户做出网络访问的提示。
请求超时设置
timeout和参数都是在nsurlrequest/nsmutableurlrequest设置的
[list=1]
nsmutableurlrequest *request = [client requestwithmethod:@"get" path:@"/" parameters:nil];//这里的parameters:参数就是你的第二个问题如何设置参数
[request settimeoutinterval:120];
afhttprequestoperation *operation = [client httprequestoperationwithrequest:request success:^{...} failure:^{...}];
[client enqueuehttprequestoperation:operation];
如果你是继承了afhttpclient
就需要override一个方法requestwithmethod
- (nsmutableurlrequest *)requestwithmethod:(nsstring *)method path:(nsstring *)path parameters:(nsdictionary *)parameters{
nsmutableurlrequest *request = [super requestwithmethod:method path:path parameters:parameters];
[request settimeoutinterval:15];
return request; }
这个时候的参数设置是调用
[self postpath:@"" parameters:nil //参数
success:^(afhttprequestoperation *operation, id responseobject) {
if (success) {
success((afjsonrequestoperation *)operation, responseobject);
}
} failure:^(afhttprequestoperation *operation, nserror *error) {
if (failure) {
failure((afjsonrequestoperation *)operation, error);
}];
本文链接:http://www.cocoachina.com/bbs/read.php?tid=184183