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

标签 ios objective-c json web-services jsonmodel

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

最佳答案

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

关于ios - 如何通过 Post 方法使用 JSON 模型将 JSON 字典作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32367406/

相关文章:

objective-c - Objective-C 模式中的 RAII?

javascript - ajax 发送 POST 两次,从 PHP 接收双重/重复结果

ios - 查找传递给函数的泛型类型

ios - 无论文本宽度如何,如何使 UIButton 标题标签字体大小相同

iphone - 如何判断 AirPrint 打印机是否可用?

ios - 同步执行 RACSignal

javascript - 访问 JSON 文件时出现问题?

json - 为Grails 2.4.X配置HAL JSON渲染器

ios - main.m中的Xcode 4开发人员信息

基于UIView动态显示的IOS键盘语言