-1

i am trying to send json array data to server but it show the result is failed. so plaese correct me where i did wrong.Here is code what i am using in this:

NSDictionary *dict=@{@"groupmembersarray":contactsArray};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                    options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [NSString stringWithFormat:@"%@",jsonString];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://anaadit.net/caffe/newapp/AddGroupContact.php"]];
[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn==nil) {
    NSLog(@"Connection could not be made");
} else {

    responseData = [NSMutableData new];
    NSLog(@"%@",responseData);
}

and nsurl connection delegate methods calling but the response is showing is nil . so please check and correct me .Thanks in advance.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200

3 Answers3

0

Please contact your webservice developer, he/she may have had some issue

Dipen
  • 268
  • 1
  • 14
0

Your question has been answered here: How to send POST and GET request?

Although judging from your comment, the issue isn't with the actual request but rather with the server. You wouldn't have gotten a response if your request code was faulty. You should check that the parameters are corresponding with what the server needs.

You can also check the status code of the request like so:

[(NSHTTPURLResponse*)response statusCode]
Community
  • 1
  • 1
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
0

Use AFNetworking frame work instead, easy to use and implement. https://github.com/AFNetworking/AFNetworking

POST request in AFNetworking -

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *dict=@{@"groupmembersarray":contactsArray};
 manager.requestSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://anaadit.net/caffe/newapp/AddGroupContact.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

To set ant http header field you can use,

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];