简体   繁体   中英

How to create and post json to web service Objective c

I try to convert NSDictionary to JSON data and sent it to PHP .server in "POST" request with setHTTPBody . I received a null from the server when I sent from my app , but when I send the JSON from PostMan I receive the objects.

Where am I wrong ?

- (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 the json i need to post.

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

jsonData1 is not nil. jsonData1不是零。 didReceiveData中的数据是:

the data in didReceiveData is :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM