如何创建和发布json到Web服务目标c

时间:2016-03-02 10:01:27

标签: objective-c nsjsonserialization nsmutableurlrequest

我尝试将NSDictionary转换为JSON数据,然后使用PHP将其发送到setHTTPBody。服务器的“POST”请求中。 当我从app发送邮件时,我从服务器收到了空值,但是当我从JSON发送PostMan时,我收到了对象。

我哪里错了?

- (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");
}

这是我需要发布的json。

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

jsonData1不是零。  jsonData1 is not nil. the data in didReceiveData is :

didReceiveData中的数据是:

1 个答案:

答案 0 :(得分:1)

尝试 AFNetworking

修改

    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];

希望这有帮助