I want to pass a JSON dictionary as a parameter by using uploadUrl
, but it gives me an Unsupported Url
error with code-1002.
When I hit this URL on Postman, it works perfectly. How do I implement this using a JSON model?
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
This method solved this issue. Previously I was assigning the unsupported 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);
}
}];