2

I want to post data through json webservicesmy on button click Action code is:

-(IBAction)login:(id)sender {
    NSString *newurlString = [NSString StringWithFormat:@"{\"name\":\"asit\",\"email\":\"m.kulkarni@easternenterprise.com\",\"businessType\":\"1\",\"score\":30}"];
    NSString * url = @"http://www.nieuwe-dag.nl/mobile_be/public/?action=saveScore";
   
    NSData *postData = [newurlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSLog(@"urlString::%@",newurlString);
    NSLog(@"postLength::%@",postLength);

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];


    if( theConnection ) {
        webData = [[NSMutableData data] retain];
    }
    else {
        NSLog(@"theConnection is NULL");
    }
}

And response on console is:

[Session started at 2011-07-25 12:36:48 +0530.] 2011-07-25 12:36:48.860 DepartmentalStoreAdmin[2490:207] Could not load the "header.png" image referenced from a nib in the bundle with identifier "com.easternenterprise.dag" 2011-07-25 12:36:48.864 DepartmentalStoreAdmin[2490:207] Could not load the "leher.png" image referenced from a nib in the bundle with identifier "com.easternenterprise.dag" 2011-07-25 12:36:50.708 DepartmentalStoreAdmin[2490:207] urlString::{"name":"asit","email":"m.kulkarni@easternenterprise.com","businessType":"1","score":30} 2011-07-25 12:36:50.709 DepartmentalStoreAdmin[2490:207] postLength::88 2011-07-25 12:36:52.376 DepartmentalStoreAdmin[2490:207] DONE. Received Bytes: 52 2011-07-25 12:36:52.377 DepartmentalStoreAdmin[2490:207]

THEXML {"result":"ERROR","errors":["Name cannot be empty"]}

I don't have any idea why it is not post.

halfer
  • 19,824
  • 17
  • 99
  • 186
pinku
  • 87
  • 1
  • 7

1 Answers1

3

Your newurlString must contain the variable name. It will be OK if you add percentage engoding too. For example:

NSString *newUrlString = [[NSString stringWithFormat:@"&json={\"name\":\"asit\",\"email\":\"m.kulkarni@easternenterprise.com\",\"businessType\":\"1\",\"score\":30}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

If you don't see any changes, here they are:

  1. Thre is &json= at the begining of the string. Change it to your real variable name. E.g. if your server checks for json in $_POST['data'], you should use &data=.
  2. There is stringByAddingPercentEscapesUsingEncoding which adds percent escapes (e.g space is converted to %20)

EDIT: Sorry, here is the proper code:

NSString *newUrlString = [NSString stringWithFormat:@"&json=%@",[[NSString stringWithFormat:@"{\"name\":\"asit\",\"email\":\"m.kulkarni@easternenterprise.com\",\"businessType\":\"1\",\"score\":30}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
akashivskyy
  • 44,342
  • 16
  • 106
  • 116