在 afnetworking 3.0 中发布请求?
Posted
技术标签:
【中文标题】在 afnetworking 3.0 中发布请求?【英文标题】:Post request in afnetworking 3.0? 【发布时间】:2016-06-08 13:08:04 【问题描述】:我有一个应用程序,我在其中使用 Afnetnetworking3.0
进行这样的 Web 服务调用。
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
NSURL * urlStr = [NSURL URLWithString:path];
NSDictionary *parameters = @@"ID":@"fdfdfd",@"No":@"1414";
NSLog(@"%@",parameters);
[manager POST:urlStr.absoluteString
parameters:parameters
progress:nil
success:^(NSURLSessionDataTask *task, id responseObject)
NSLog(@"JSON: %@", responseObject);
failure:^(NSURLSessionDataTask *task, NSError *error)
NSLog(@"Error: %@", error);
];
但它在参数字典中添加了一些空格并导致找不到 URL。谁能告诉我哪里出错了?
【问题讨论】:
您的代码看起来不错。不过你的网址可能有问题。 @NeilGaliaskarov 我们将如何使用参数打印最终路径 @hacker 首先,从这里打印路径NSURL * urlStr = [NSURL URLWithString:path];
并将日志结果添加到您的问题中
我已经打印了路径字符串,也一样
【参考方案1】:
试试这样:
NSString *Loginurl = [NSString stringWithFormat:@"http://Enter your url here"];
NSDictionary *params = @
@"ID":@"fdfdfd",
@"No":@"1414"
;
NSLog(@"Sent parameter to server 1 : %@",params);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
[policy setValidatesDomainName:NO];
[policy setAllowInvalidCertificates:YES];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",nil];
[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject)
NSLog(@"Response from server 1 : %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
[self getdata:responseObject];
failure:^(NSURLSessionTask *operation, NSError *error)
NSLog(@"Error: %@", error);
];
-(void)getdata:(NSDictionary*)RegisterData
检查日志Sent parameter to server 1
...并检查您的网址是否正确?
【讨论】:
以上是关于在 afnetworking 3.0 中发布请求?的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking 3.0 GET 请求在 iOS 中保存到类中
AFNetworking 3.0 发送请求正文内容 [重复]