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(@"請求失敗");//訪問失敗調用
}];
}