2013-05-30 49 views
1

我正試圖發送發送錯誤的發送請求到服務器的JSON數據。我在下面的格式創建數據在發送請求中的iphone中發送json數據的問題iphone

 NSDictionary *o1 = [[NSMutableDictionary alloc] init]; 
     [o1 setValue:@"51" forKey:@"merchantProductId"]; 
     [o1 setValue:@"Big Paulie" forKey:@"name"]; 
     [o1 setValue:@"1" forKey:@"quantity"]; 
     NSDictionary *o2 = [[NSMutableDictionary alloc] init]; 
     [o2 setValue:@"52" forKey:@"merchantProductId"]; 
     [o2 setValue:@"Paulie" forKey:@"name"]; 
     [o2 setValue:@"10" forKey:@"quantity"]; 

     NSMutableArray *pizzas = [NSMutableArray arrayWithObjects:o1, o2, nil]; 
     NSDictionary *o3 = [[NSMutableDictionary alloc] init]; 
     [o3 setValue:@"3" forKey:@"merchantId"]; 
     [o3 setValue:pizzas forKey:@"pizzas"]; 
     NSMutableArray *orderArray = [NSMutableArray arrayWithObjects:o3, nil]; 
     NSData *jsData = [NSJSONSerialization dataWithJSONObject:orderArray options:NSJSONWritingPrettyPrinted error:nil]; 
NSString *data = [NSString stringWithFormat:@"data=%@",[[NSString alloc] initWithData:jsData                     encoding:NSUTF8StringEncoding]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:61.12.124.234:60/upload_image/phoneTesting.php"]]]; 
    [request setHTTPMethod:@"POST"]; 
    // [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 

    NSHTTPURLResponse* urlResponse = nil; 
    error = [[NSError alloc] init]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    NSLog(@"Response: %@", result); 

服務器的,我需要的數據應該出現像

 { 
    "pizzas" : [ 
     { 
     "quantity" : "1", 
     "merchantProductId" : "51", 
     "name" : "Big Paulie" 
     }, 
     { 
     "quantity" : "10", 
     "merchantProductId" : "52", 
     "name" : "Paulie" 
     } 
    ], 
    "merchantId" : "3" 
    } 

但我正在逐漸

(
    [data] => [ 
    { 
    "pizzas" : [ 
     { 
     "quantity" : "1", 
     "merchantProductId" : "51", 
     "name" : "Big Paulie" 
     }, 
     { 
     "quantity" : "10", 
     "merchantProductId" : "52", 
     "name" : "Paulie" 
     } 
    ], 
    "merchantId" : "3" 
    } 
] 
) 

,當我試圖只送jsData它不會發送任何與請求相關的東西。

請建議我如何發送發佈請求中唯一的json數據。

我使用xcode4.5

+0

'postData'初始化在哪裏? – Mar0ux

+0

上面有幾個值,沒有明顯的初始化。目前還不清楚jsData如何將其納入請求。請提供完整的代碼。 –

回答

0

我不知道爲什麼你做下面一行:

NSString *data = [NSString stringWithFormat:@"data=%@", 
    [[NSString alloc] initWithData:jsData encoding:NSUTF8StringEncoding]]; 

如果這是一個GET請求,那麼你需要一個鍵值配對,但你只是發送POST數據,它不需要鑰匙。

我認爲這就是爲什麼你會得到那個白眼[data] = entry。如果你做到了:

NSString *data = [NSString stringWithFormat:@"%@", 
    [[NSString alloc] initWithData:jsData encoding:NSUTF8StringEncoding]]; 

它工作嗎?

+0

看不出如何工作,因爲「數據」顯然永遠不會被使用。 –

+0

是的,你是對的。如果數據真的是postdata,那麼我可能在這裏有一些東西。看起來他認爲他需要使用POST有效負載的GET語法。 –

0

它看起來對我像你序列化在此行錯了對象:

NSData *jsData = [NSJSONSerialization dataWithJSONObject:orderArray 
    options:NSJSONWritingPrettyPrinted error:nil]; 

你爲什麼序列化包含字典中的陣列?當然,你應該只是序列化字典本身。

NSData *jsData = [NSJSONSerialization dataWithJSONObject:o3 
    options:NSJSONWritingPrettyPrinted error:nil];