How to create and send json to web service Goal c

I am trying to convert data NSDictionaryto JSONand send it to PHP.server in a "POST" request with setHTTPBody. I received zero from the server when I sent from mine app, but when I send JSONfrom PostMan, I get objects.

Where am I mistaken?

- (void)viewDidLoad
{
   [super viewDidLoad];

NSError *error = nil;

NSString *url = [NSString stringWithFormat:@"http://myAddress/sql_service.php"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

NSArray *arrayOfStrings = @[@"alex",@"dima"];
NSDictionary *dict = @{@"request_type" : @"select_with_params",
                           @"table" : @"user",
                           @"where" : @"f_name=? OR f_name=?",
                           @"values" : arrayOfStrings};

NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];

[request setHTTPBody:jsonData1];

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

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData    *)data
{
if (data)
{
    [receivedData appendData:data];
}
else
{
}
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
  NSLog(@"didFailWithError");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  NSError * error = nil;
  NSMutableDictionary *dictionary = [NSJSONSerialization       JSONObjectWithData:receivedData options:0 error:&error];
  NSLog(@"connectionDidFinishLoading");
}

This is json I need to publish.

{
  request_type: "select_with_params",
  table: "user", 
  where: "f_name=? OR f_name=?", 
  values: ["dima", "alex"]
}

jsonData1 is not zero. jsonData1 is not zero. data in didReceiveData:

data in didReceiveData:

+4
source share
1 answer

Try AFNetworking

EDIT

    NSString *url = [NSString stringWithFormat:@"http://myAddress/sql_service.php"];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    NSArray *arrayOfStrings = @[@"alex",@"dima"];
    NSDictionary *dict = @{@"request_type" : @"select_with_params",
                               @"table" : @"user",
                               @"where" : @"f_name=? OR f_name=?",
                               @"values" : arrayOfStrings};

    NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
    [request setHTTPBody: [[NSString stringWithFormat:@"%@", jsonData1] dataUsingEncoding:NSUTF8StringEncoding]];

    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){

if (responseObject)
{
    NSLog(@"Success!");
}} failure:^(AFHTTPRequestOperation *operation, NSError *error)             {
    NSLog(@"Error");
    }];

[op start];

Hope this helps

+1
source

All Articles