Reputation: 791
- (NSDictionary*)PostWebService:(NSString*)completeURL param:(NSString*)value
{
@try
{
NSString *urlStr =[completeURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
//NSString *urlPart=@"req=value";
//NSString *urlPart;
NSString *urlPart=[NSString stringWithFormat:@"req=%@", value];
NSLog(@"String %@",urlPart);
NSData *requestBody = [urlPart dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"String %@",requestBody);
[request setHTTPBody:requestBody];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
NSLog(@"String %@",responseString);
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
return json;
}
@catch(NSException *e)
{
NSLog(@"reason is%@",e.reason);
}
}
and i call this method here..
-(NSDictionary*)gotvall:(NSString*)req
{
@try
{
NSString *vurl=@"some url/";
// vurl=[vurl stringByAppendingString:@"req="];
// vurl=[vurl stringByAppendingString:req];
// NSLog(@"%@",vurl);
NSDictionary *json=[self PostWebService:vurl param:req];
NSLog(@"json is%@",json);
return json;
}
@catch(NSException *e)
{
NSLog(@"%@",e.reason);
}
}
After debugging this method I got result as data parameter is nil
.
Can anyone tell that what I am doing wrong here.
I got the complete url and when I run that url on browser I got the perfect data but when I printing the value of json it returns null
.
Upvotes: 1
Views: 687
Reputation: 437422
You reported that your error was:
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x8d84960 {NSErrorFailingURLStringKey=%20http://www.hugosys.in/www.nett-torg.no/api/vehicle/, NSErrorFailingURLKey=%20http://www.hugosys.in/www.nett-torg.no/api/vehicle/, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x8d8bfe0 "unsupported URL"}
That %20
in your error message is a space at the start of your URL that your stringByAddingPercentEscapesUsingEncoding
call converted to %20
. If you remove that extra space, you should be in good shape.
A couple of other observations:
Just to warn you, your use of stringByAddingPercentEscapesUsingEncoding
will correctly handle the presence of a space in the req
value. But it will not properly handle the presence of certain other characters (notably &
or +
). If it's possible that those sorts characters might appear in the req
value, you might want to remove the call to stringByAddingPercentEscapesUsingEncoding
for the whole URL, and instead just use CFURLCreateStringByAddingPercentEscapes
(which gives you a little more control over the percent escaping process) on just the req
value. See the percentEscapeString
method in this answer: Append data to a POST NSURLRequest.
In both your network request as well as your JSON parsing process, you are returning an NSError
object. I might suggest that you log those values if they're ever non-nil
, which will help you diagnose problems in the future.
I notice that you are using exception handling. That's not common in Objective-C. As the Programming in Objective-C guide says:
Dealing with Errors
Almost every app encounters errors. Some of these errors will be outside of your control, such as running out of disk space or losing network connectivity. Some of these errors will be recoverable, such as invalid user input. And, while all developers strive for perfection, the occasional programmer error may also occur.
If you’re coming from other platforms and languages, you may be used to working with exceptions for the majority of error handling. When you’re writing code with Objective-C, exceptions are used solely for programmer errors, like out-of-bounds array access or invalid method arguments. These are the problems that you should find and fix during testing before you ship your app.
All other errors are represented by instances of the
NSError
class. This chapter gives a brief introduction to usingNSError
objects, including how to work with framework methods that may fail and return errors. For further information, see Error Handling Programming Guide.
Bottom line, As I mentioned in the second point, you should be checking NSError
return values yourself rather than relying on exceptions in Objective-C.
Upvotes: 2