2016-03-02 80 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 –

相關問題