当我请求网络时:
[Mysevers AFPOSTWithHud:YES andAddressname:@"Common/Currency/Register" parmas:params RequestSuccess:^(id result) {
if ([result[@"status"] intValue] == 200) {
[SVProgressHUD showSuccessWithStatus:@"注册成功"];
UIViewController *temp_vc = nil;
for (UIViewController *vc in self.navigationController.childViewControllers) {
if ([vc isKindOfClass:[HomeVC class]] ) {
temp_vc = vc;
break;
}
}
if (temp_vc == nil) {
return;
}
[self.navigationController popToViewController:temp_vc animated:YES];
}else {
[SVProgressHUD showErrorWithStatus:@"注册失败"];
}
} failBlcok:^{
SVNoNet;
}];
我在申请网络时遇到错误:
[Mysevers AFPOSTWithHud:andAddressname:parmas:RequestSuccess:failBlcok:] _ block_invoke.47] [行号:57]错误==无法读取数据,因为格式不正确。
Mysevers代码如下:
@implementation Mysevers
+ (AFHTTPSessionManager *)sharedHTTPSession{
static dispatch_once_t onceToken;
static AFHTTPSessionManager *requestManager ;
dispatch_once(&onceToken, ^{
requestManager = [AFHTTPSessionManager manager];
requestManager.requestSerializer.timeoutInterval = 10;
requestManager.requestSerializer = [AFJSONRequestSerializer serializer]; // 添加,否则会返回500错
requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
});
return requestManager;
}
+(void)AFPOSTWithHud:(BOOL)hud andAddressname:(NSString*)addressName parmas:(NSDictionary*)parmas RequestSuccess:(void(^)(id result))success failBlcok:(void(^)(void))failBlcok
{
if (hud) {
//[HUD addHUD];
[SVProgressHUD show];
}
AFHTTPSessionManager *requestManager = [Mysevers sharedHTTPSession];
//requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSString *urlStr = [NSString stringWithFormat:@"%@%@",BASE_URL,addressName];
DLog(@"%@",urlStr);
[requestManager POST:urlStr parameters:parmas progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (hud) {
//[HUD removeHUD];
[SVProgressHUD dismiss];
}
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error != nil) {
DLog(@"error==%@",[error localizedDescription]);
if (hud) {
//[HUD removeHUD];
[SVProgressHUD dismiss];
}
failBlcok();
}
return ;
}];
}
注意,我的acceptedContentTypes如下:
requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil]
更新
我将sharedHTTPSession更改为:
+ (AFHTTPSessionManager *)sharedHTTPSession{
static dispatch_once_t onceToken;
static AFHTTPSessionManager *requestManager ;
dispatch_once(&onceToken, ^{
requestManager = [AFHTTPSessionManager manager];
requestManager.requestSerializer.timeoutInterval = 10;
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setStringEncoding:NSUTF8StringEncoding];
requestManager.requestSerializer=serializer;
//requestManager.requestSerializer = [AFJSONRequestSerializer serializer]; // 添加,否则会返回500错
requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
});
return requestManager;
}
仍然会收到此错误。
PHPer打印$_POST
,它是一个空数组。
(lldb) po result
{
data = "";
msg = 2001;
status = 400;
}
结果是PHPer返回给我,msg
是他定义的,只有$_POST
是空的。
注意-2
在我的AFHTTPSessionManager.m
中,我在请求标题中添加了一行:
#pragma mark - add by me
[request addValue:API_KEY forHTTPHeaderField:@"PUBLICKEY"];
在我的AFHTTPSessionManager.m
:
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(id)parameters
uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
success:(void (^)(NSURLSessionDataTask *, id))success
failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
NSError *serializationError = nil;
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
#pragma mark - add by me
[request addValue:API_KEY forHTTPHeaderField:@"PUBLICKEY"];
if (serializationError) {
if (failure) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
#pragma clang diagnostic pop
}
return nil;
}
__block NSURLSessionDataTask *dataTask = nil;
dataTask = [self dataTaskWithRequest:request
uploadProgress:uploadProgress
downloadProgress:downloadProgress
completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) {
failure(dataTask, error);
}
} else {
if (success) {
success(dataTask, responseObject);
}
}
}];
return dataTask;
}
更新-3
我将所有打印信息都放在下面:
2017-05-06 18:03:31.685086 KYGM [15582:531703] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器是/ Users / luowensheng / Library / Developer / CoreSimulator / Devices / 2FACECB7- 2E7A-4E8C-8AF5-179ED7344D93 /数据/容器/共享/ SystemGroup / systemgroup.com.apple.configurationprofiles
2017-05-06 18:03:31.686106 KYGM [15582:531703] [MC]从私人有效用户设置中读取。
2017-05-06 18:03:39.568 KYGM [15582:531703] [文件名:/Users/luowensheng/Desktop/company_project/KYGM/KYGM/Utils/LML/LMLUtils/Mysevers.m] [函数名:+ [Mysevers AFPOSTWithHud:andAddressname:parmas:RequestSuccess:failBlcok:]] [行号:47] http://192.168.3.103:8003/Common/Currency/Register
更新-04
如果我使用postman
请求,我会成功。
我不知道表单数据是否是原因。
答案 0 :(得分:0)
我发现你的邮递员的参数数据类型是form-data
,所以我认为你应该使用AFNetworking的表单提交方法:
尝试以下代码:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
NSDictionary *params = @{
@"UserName":@"admin5",
@"Password":@"123456"
};
[manager POST:@"your url" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSLog(@"formData=%@", formData);
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject=%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error=%@", error);
}];