url出现字典问题的解决方案
今天朋友给了我一个url接口,用AFNetwoing发送请求,发送请求争取的就返回下面的正确提示,否则是错误提示.
AFN:
请求路径:
http://139.196.252.209:88/apicenter?code=sendCode&data={tel:13025898989,codeType:1,devType:2}
正确返回结果:
{"result":"0","msg":"账号已使用","code":null,"data":null}
错误返回结果:
{"result":"0","msg”:”请求参数错误”,”code":null,"data":null}
- ###解决办法
- (void)post
{
//AFHTTPSessionManager内部包装了NSURLSession(现在AFNetworking已经放弃了对NSURlConnection的封装)
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
//把data参数(也就是字典作为整体),看做一个整体的字符串进行拼接到请求中
NSString *arr = @"{tel:13025898989,codeType:1,devType:2}";
//把data参数以字典作为整体进行拼接是错误的,因为拼接之后,增加了好多非url字符进去了
//NSDictionary *dit = @{@"tel": @"13025898989",@"codeType":@"1",@"devType":@"2"};
NSDictionary *paraDict = @{
@"code":@"sendCode",
@"data":arr,
};
//发送请求
[mgr POST:@"http://139.196.252.209:88/apicenter" parameters:paraDict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSLog(@"%@",formData);
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);//返回的数据
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");//访问失败调用
}];
}