如何通过 Post 方法使用 JSON 模型将 JSON 字典作为参数传递?

How to pass JSON dictionary as parameter using JSON Model by Post method?

我想使用 uploadUrl 传递一个 JSON 字典作为参数,但它给我一个 Unsupported Url 代码为 1002 的错误。

当我在 Postman 上点击这个 URL 时,它运行良好。我如何使用 JSON 模型实现它?

NSString *uploadUrl =@"<Your host URL>";

[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:nil
                               completion:^(NSDictionary *json, JSONModelError *err)
{
    if(err == nil)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        completionHanldler(json);
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

        NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
        if(err.code==-1009)
            [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
        else
            [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

        completionHanldler(errorDict);
    }
}];

试试这个:

NSString *uploadUrl =@"<Your host URL>";
NSDictionary *val = @{<Add your params as key : value here>};
NSArray *innerParameter = @[val];
NSDictionary *parameter =@{@"Passports":innerParameter};


[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:parameter
                               completion:^(NSDictionary *json, JSONModelError *err)
{
    if(err == nil)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        NSError *error;
        NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:objectData  options:NSJSONReadingMutableContainers error:&jsonError]; // this is a dictionary NOT the JSON
        completionHanldler(resultDict);
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

        NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
        if(err.code==-1009)
            [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
        else
            [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

        completionHanldler(errorDict);
    }
}];

编辑:

使用 AFNetworking 的同一网络调用

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *val = @{<Params as key : value>};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"<your host URL>" parameters:val success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

编辑 2:

首先,我认为后端是在 POST 中完成的,但它正在通过 URL 编码。经过多次试验和错误后,这无法完成,即使在 GET 中尝试之后也是如此。据我所知,后端无法识别 JSON 格式的 POST 方法,因此您必须仅以 URL 编码方式发送它。

正在审查您的问题并API。你想发送一些信息到后端,你需要使用 POST 方法 POST 信息和你的数据 json 格式。

您可以使用任何在线 API 代理(例如 POSTMANhurlit)测试请求。

希望您清楚,您不能使用 GET 请求发送超过 256 个字节,因为您的 API 是 POST 请求。在您现有的方法中进行以下更改,您就完成了。

NSString *uploadUrl =@"<Your host URL>";
NSDictionary *dictParams = @{@"key" : @"value"};
[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:dictParams
                               completion:^(NSDictionary *json, JSONModelError *err)
 {
 }];

stringByAddingPercentEscapesUsingEncoding

这个方法解决了这个问题。以前我正在分配不受支持的 URL。

NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&err];
NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// This line is the answer.
myString = [myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

NSString *uploadUrl = [NSString stringWithFormat:@"<MY host URL>"?data=%@",myString];

[JSONHTTPClient postJSONFromURLWithString:uploadUrl params:nil
                               completion:^(NSDictionary *json, JSONModelError *err)
{
    if(err == nil)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"success" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        completionHanldler(json);
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed" message:@"uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

        NSMutableDictionary *errorDict=[[NSMutableDictionary alloc]init];
        if(err.code==-1009)
            [errorDict setObject:@"The Internet connection appears to be offline."forKey:@"error"];
        else
            [errorDict setObject:@"Error occurred. Please try again!"forKey:@"error"];

        completionHanldler(errorDict);
    }
}];