iOS开发 AFNetWorking封装的方法,block返回值赋值问题
请求成功block里面的返回值backinfo,将backinfo赋值给一个变量projectBookList,projectBookList永远为空。梗了好久都找不到办...
请求成功block里面的返回值backinfo,将backinfo赋值给一个变量projectBookList,projectBookList永远为空。梗了好久都找不到办法,求助大神!!
展开
3个回答
展开全部
您好,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];然后将返回的数据自己转换。
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。找不到办法!
展开全部
#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
#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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我认为block就是一个指针,不过所指向的是一个函数,使用这个指针可以调用也就是执行那个函数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询