【发布时间】: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