1
- (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.

Rudra
  • 791
  • 7
  • 21
  • 2
    If `responseData` was `nil`, did you examine `requestError`? What did it contain? BTW, you're adding `req=` to the URL as well as the `requestBody`. For `POST` request, you probably want it solely in the body. If `req` contains any reserved characters (e.g. space, `&`, `+`, etc.), you'll want to percent-escape it, as well. – Rob Apr 01 '14 at 05:07
  • 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"} – Rudra Apr 01 '14 at 05:09
  • request error contains the above commented data – Rudra Apr 01 '14 at 05:09
  • Thankx bro..thankx a ton for this help... – Rudra Apr 01 '14 at 05:15
  • can you paste the responseString here? Or else you can write the responseData to a file : [reponseData writeToFile: atomically:YES]. – Aneesh Dangayach Apr 01 '14 at 05:16
  • To check whether it is valid json response or not, try on http://jsonlint.com/ – Aneesh Dangayach Apr 01 '14 at 05:17

1 Answers1

2

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/vehicl‌​e/, 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:

  1. 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.

  2. 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.

  3. 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 using NSError 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.

Community
  • 1
  • 1
Rob
  • 371,891
  • 67
  • 713
  • 902