我想使用 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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |