2016-03-02 79 views
3

我尝试将NSDictionary转换为JSON数据并将其发送到PHP .server,并在setHTTPBody的“POST”请求中发送。 当我从我的app寄出时,我从服务器收到一个空,但是当我从PostMan发送JSON时,我收到这些对象。如何创建JSON并将其发布到Web服务客观c

我在哪里错了?

- (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的数据是:

+0

您是否已经验证ŧ帽子'jsonData1'不是'nil'? – Avi

+0

究竟是什么错误?在'NSURLConnectionDelegate'的方法中,你是否收到任何数据?如果是你的回答是顶层的NSArray而不是NSDictionary? – Larme

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

希望这有助于

+0

当我将AFNetworking添加到我的项目后,我得到了http://puu.sh/ntaWx/8d288a851f.png我需要添加哪个框架工作? –

+1

你导入了“AFHTTPRequestOperationManager.h”吗? –

+0

AFHTTPRequestOperationManager.h身份不明http://puu.sh/ntdMx/6b40bb9df0.png –

相关问题