iOS开发 AFNetWorking封装的方法,block返回值赋值问题

请求成功block里面的返回值backinfo,将backinfo赋值给一个变量projectBookList,projectBookList永远为空。梗了好久都找不到办... 请求成功block里面的返回值backinfo,将backinfo赋值给一个变量projectBookList,projectBookList永远为空。梗了好久都找不到办法,求助大神!! 展开
 我来答
山水阿锐
2015-04-07 · TA获得超过34.3万个赞
知道顶级答主
回答量:23.7万
采纳率:91%
帮助的人:3.2亿
展开全部
您好,AFHTTPClient处理GET 和 POST请求.做网页的朋友们这个方法用的比较多。在要经常调用某个请求时,可以封装,节省资源。
BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:BaseURLString]];
NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"json" forKey:@"format"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"text/html"];
[client postPath:@"weather.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, idresponseObject) {
NSString* newStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"POST请求:%@",newStr);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];

[client getPath:@"weather.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, idresponseObject) {
NSString* newStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"GET请求:%@",newStr);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];

运行结果:

如果需要显示网络活动指示器,可以用下面方法:
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x16774de0 {NSErrorFailingURLKey=http://192.168.2.2:8181/ecar/tsp/uploadLocation?CID=781666&serviceType=1, AFNetworkingOperationFailinponseErrorKey= { URL: http://192.168.2.2:8181/ecar/tsp/uploadLocation?CID=781666&serviceType=1 } { status code: 200, headers {
XXX

} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
返回数据格式不对。注销这句话: op.responseSerializer = [AFJSONResponseSerializer serializer];然后将返回的数据自己转换。
追问
就是在block中将idresponseObject赋值给一个全局变量用于之后的tableview显示,但是只要出了block,被赋值的全局变量就为nil。找不到办法!
雪花飞落人家
2015-10-18 · TA获得超过403个赞
知道小有建树答主
回答量:712
采纳率:0%
帮助的人:514万
展开全部
#import <Foundation/Foundation.h>

#import "AFNetworking.h"

typedef void (^HttpSuccess)(id json);

typedef void (^HttpFailure)(NSError *error);

@interface Tool : NSObject

+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(HttpSuccess)success failure:(HttpFailure)failure;

+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(HttpSuccess)success failure:(HttpFailure)failure;

//

// Tool.m/

//

#import Tool.h"

@implementation MBBusHttpTool

+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(MBBusHttpSuccess)success failure:(MBBusHttpFailure)failure

{

[self postRequestWithMethod:@"POST" url:url params:params success:success failure:failure];

}

+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(MBBusHttpSuccess)success failure:(MBBusHttpFailure)failure

{

[self getRequestWithMethod:@"GET" url:url params:params success:success failure:failure];

}

+ (void)postRequestWithMethod:(NSString *)method url:(NSString *)url params:(NSDictionary *)params success:(HttpSuccess)success failure:(HttpFailure)failure

{

AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];

[manger POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

if (success)

{

success(responseObject);

}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

if (error)

{

failure(error);

}

}];

}

+ (void)getRequestWithMethod:(NSString *)method url:(NSString *)url params:(NSDictionary *)params success:(HttpSuccess)success failure:(HttpFailure)failure

{

AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];

[manger GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

if (success)

{

success(responseObject);

}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

if (error)

{

failure(error);

}

}];

}

@end
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
胜元哎1620
2015-09-25 · TA获得超过136个赞
知道答主
回答量:235
采纳率:0%
帮助的人:63万
展开全部
我认为block就是一个指针,不过所指向的是一个函数,使用这个指针可以调用也就是执行那个函数。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式