0

I am trying to send JSON data to server, but it is not go to server. I am getting nil data to print from server side but no use . Here I am using code for post data to server

NSError *error;
NSDictionary *dict=@{
                     @"allgroups": @{
                             @"groupname": @"prasad",
                             @"group_id":@"26",
                             @"user_id":@"8",
                             @"contacts": @[contactsArray]
                             }
                     };

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict  
                                                   options:NSJSONWritingPrettyPrinted 
                                                     error:&error];

NSString *saveString = [[NSString alloc] initWithData:jsonData 
                                             encoding:NSUTF8StringEncoding];

NSString *myRequestString = [NSString stringWithFormat:@"%@",saveString];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat:@"http://example.php"]]];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];

//post section
[request setHTTPBody: myRequestData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];

NSLog(@"String value is:: %@",returnString);

please help me.thanks in advance

soumya
  • 3,801
  • 9
  • 35
  • 69
  • 1
    Strange `NSData` -> `NSString` -> `NSString` (copy) -> `NSData`. – Droppy Oct 06 '15 at 10:17
  • `contactsArray` is a `NSArray`? You're creating a `NSArray` with only one object, a other `NSArray` ? Just pointing out that could be an issue. Else, what's nil exactly? What shows "err"? Plus as said by Droppy, there is weird "circle" transformations. – Larme Oct 06 '15 at 10:20
  • I followed you're guideline but still i am getting response like :NIL" only in server side i use only "print" function nothin else – guruprasad gudluri Oct 06 '15 at 10:27
  • @Larme :contactsArray is nsmutablearray and it has 6 key value pairs – guruprasad gudluri Oct 06 '15 at 10:30
  • please check my code in "http://pastie.org/private/e7ualpuhknlswytp4ocijg" and help me bro's – guruprasad gudluri Oct 06 '15 at 10:46
  • Possible duplicate of [Post data in Objective C using Json](http://stackoverflow.com/questions/9883081/post-data-in-objective-c-using-json) – rkyr Oct 06 '15 at 11:59

2 Answers2

0

You should directly pass your jsonData to NSMutableURLRequest like this:

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat:@"http://example.php"]]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPMethod: @"POST"];
[request setHTTPBody:jsonData];
Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

Following function is working for me .You can use it:

-(NSMutableArray*)Post_method:(NSString*)post_string posturl:(NSString *)post_url
{
    NSString *post =post_string;
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseURL,post_url]];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSError *error;
    NSURLResponse *response;
    NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString* aStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
    SBJSON *json=[[SBJSON alloc]init];
    NSMutableArray *resultp=[json objectWithString:aStr];
    //NSLog(@"result   %@ ",resultp);
    return  results;
}

And call this function as:

[self Post_method:[NSString stringWithFormat:@"%@",jsonString] posturl:@""];
David
  • 3,285
  • 1
  • 37
  • 54
Ankit Saini
  • 284
  • 2
  • 14
  • In my situation, adding [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; solved the problem. – AndaluZ Dec 05 '15 at 21:59