4

I try to convertNSDictionary 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 is not nil. the data in didReceiveData is :

the data in didReceiveData is :

Roei Nadam
  • 1,780
  • 1
  • 15
  • 33
  • Have you verified that `jsonData1` isn't `nil`? – Avi Mar 02 '16 at 10:05
  • What is wrong exactly? In the methods of `NSURLConnectionDelegate`, do you receive any data? Could if be that your response is a `NSArray` at top level and not a `NSDictionary`? – Larme Mar 02 '16 at 10:11
  • I edit the question and add image the is not nil. – Roei Nadam Mar 02 '16 at 10:11
  • I edit the question again. – Roei Nadam Mar 02 '16 at 10:20
  • Is `connection:connectionDidFinishLoading:` called? If YES, could you log the value of [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]`, or even each time the delegate method `connection:didReceiveData:` is called? – Larme Mar 03 '16 at 20:55

1 Answers1

1

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

Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34
  • after i add AFNetworking to my project , a I get http://puu.sh/ntaWx/8d288a851f.png which frame work I need to add ? – Roei Nadam Mar 03 '16 at 10:52
  • 1
    did you import "AFHTTPRequestOperationManager.h" ? – Kamlesh Shingarakhiya Mar 03 '16 at 10:58
  • I fix this problem, but in your code have a problem in - [request setHTTPBody: [jsonData1 dataUsingEncoding:NSUTF8StringEncoding]]; I try to do this but is still not work - NSData* jsonData1 = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error]; [request setHTTPBody:jsonData1]; – Roei Nadam Mar 03 '16 at 14:50
  • 1
    Try now EDITED. Just replace [request setHTTPBody: [jsonData1 dataUsingEncoding:NSUTF8StringEncoding]]; into [request setHTTPBody: [[NSString stringWithFormat:@"%@", jsonData1] dataUsingEncoding:NSUTF8StringEncoding]]; – Kamlesh Shingarakhiya Mar 04 '16 at 02:50
  • thanks you for all (-: ,but its still not works , i add the code image with the error the i give - http://puu.sh/nwD0r/a62b3c6183.png - here you can see that in "postMan" its works - http://puu.sh/nwD5w/3573303483.png – Roei Nadam Mar 06 '16 at 08:58