【问题标题】:How to pass JSON dictionary as parameter using JSON Model by Post method?如何通过 Post 方法使用 JSON 模型将 JSON 字典作为参数传递?
【发布时间】:2015-09-03 05:12:19
【问题描述】:

我想使用 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);
    }
}];

【问题讨论】:

  • 我不知道 JSONHTTPClient 但我认为在 url 中传递 json 首先需要将 json 转换为字符串,然后将字符串作为参数传递。这就是它在邮递员中成功运行的原因
  • NSError * err; NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&err]; NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];是的,我正在将其转换为字符串。但这对我不起作用
  • @SourabhSharma 绝不会公开您的域和参数详细信息。

标签: ios objective-c json web-services jsonmodel


【解决方案1】:

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);
    }
}];

【讨论】:

  • 不是很有用,因为您甚至没有在问题中显示 URL。
  • 我已经给出了网址,但有人编辑了问题。
  • 致歉;我已经编辑了你的问题,以扭转我的反对意见。
  • 感谢您的支持@trojanfoe
【解决方案2】:

查看您的问题和 API。您想向后端发送一些信息,您需要使用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)
 {
 }];

【讨论】:

    【解决方案3】:

    试试这个:

    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 编码方式发送它。

    【讨论】:

    • 你运行过这段代码吗??它显示了 innerParameter 字典中的预期表达式错误。
    • Error Domain=JSONModelErrorDomain Code=2 "网络响应错误。可能无法访问 JSON URL。" UserInfo=0x14f0c180 {NSLocalizedDescription=错误的网络响应。可能 JSON URL 无法访问。} 出现此错误??请帮忙。
    • 刚刚删除了“?”标记尝试一次,我无法达到此但尝试一次并确认请PLZ
    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2021-12-10
    • 2014-11-17
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多