2016-04-15 52 views
0

我試圖在後端數據源中存儲大量數據,但由於未知原因,REST API在存儲時將原始數據修剪爲較小的塊。無法使用Rest API在後端發佈大數據

我的Objective-C的功能,使請求,

-(NSString *)getPostAPIResponseWithDestinationString:(NSString *)destinationString AndWithRequestString:(NSString *)requestString 
{ 
    if([self isConnectedToInternet]) 
    { 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", degsChickenBackendURL, destinationString]]]; 
     //NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", degsChickenBackendURL, destinationString]]]; 

     [request setHTTPMethod:@"POST"]; 
     NSString *postRequestString = [NSString stringWithFormat:@"%@", requestString]; 
     [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postRequestString length]] forHTTPHeaderField:@"Content-length"]; 

     [request setHTTPBody:[postRequestString dataUsingEncoding:NSUTF8StringEncoding]]; 
     NSError *myError = nil; 
     NSURLResponse *response; 

     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&myError]; 
     @try 
     { 
      return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
     } 
     @catch (NSException *exception) 
     { 
      //return @"Error"; 
     } 
    } 
    else 
    { 
     return @"Error"; 
    } 
} 

原始的JSON字符串形式的數據是,

{ 
    "OrderQuantity": "1", 
    "OrderFoodDetails": { 
    "FoodName": "¼ Chicken Leg and Thigh", 
    "FoodPrice": "7.49", 
    "FoodDescription": "Includes two fresh bread rolls with butter." 
    }, 
    "OrderAddOns": [ 
    { 
     "CategorySelectionLimit": "1", 
     "CategoryCurrentValue": "Peri Peri Spice Level", 
     "CategoryHeader": "Peri Peri Spice Level", 
     "CategoryPrice": "0.000000", 
     "CategoryItems": [ 
     { 
      "ItemPrice": "0.000000", 
      "ItemHeader": "Garlic - Simplicity", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "0.000000", 
      "ItemHeader": "Lemon & Herb - Simplicity", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "0.000000", 
      "ItemHeader": "Mild Peri Peri - Try it", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "0.000000", 
      "ItemHeader": "Hot Peri Peri - Go for it", 
      "ItemSelectionState": "1" 
     }, 
     { 
      "ItemPrice": "0.000000", 
      "ItemHeader": "Extra Hot Peri Peri - Dare it", 
      "ItemSelectionState": "0" 
     } 
     ], 
     "CategorySelectionState": "0" 
    }, 
    { 
     "CategorySelectionLimit": "0", 
     "CategoryCurrentValue": "Drinks", 
     "CategoryHeader": "Drinks", 
     "CategoryPrice": "0.000000", 
     "CategoryItems": [ 
     { 
      "ItemPrice": "1.490000", 
      "ItemHeader": "Bottled Water", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.590000", 
      "ItemHeader": "Coca Cola Freestyle Large", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.290000", 
      "ItemHeader": "Coca Cola Freestyle Regular", 
      "ItemSelectionState": "0" 
     } 
     ], 
     "CategorySelectionState": "0" 
    }, 
    { 
     "CategorySelectionLimit": "10", 
     "CategoryCurrentValue": "Add Ons", 
     "CategoryHeader": "Add Ons", 
     "CategoryPrice": "0.000000", 
     "CategoryItems": [ 
     { 
      "ItemPrice": "2.990000", 
      "ItemHeader": "Deg’s Signature Wurlys", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "4.490000", 
      "ItemHeader": "Deg’s Signature Wurlys with Perinaise", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "Healthy Fries", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "3.990000", 
      "ItemHeader": "Healthy Fries with Perinais", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "Mashed Potatoes", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "Baked Potatoes", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "Coleslaw", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "Corn", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "Brown Rice", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "Perinaise", 
      "ItemSelectionState": "0" 
     }, 
     { 
      "ItemPrice": "2.490000", 
      "ItemHeader": "2 Freshly baked Dinner Rolls with Butter", 
      "ItemSelectionState": "0" 
     } 
     ], 
     "CategorySelectionState": "0" 
    } 
    ], 
    "OrderTotalPrice": "7.490000", 
    "OrderSinglePrice": "7.490000", 
    "OrderUserName": "kl's Order" 
} 

並修整大塊是,

"{\\\"OrderQuantity\\\":\\\"1\\\",\\\"OrderFoodDetails\\\":{\\\"FoodName\\\":\\\"\u00bc Chicken Leg and Thigh\\\",\\\"FoodPrice\\\":\\\"7.49\\\",\\\"FoodDescription\\\":\\\"Includes two fresh bread rolls with butter.\\\"},\\\"OrderAddOns\\\":[{\\\"CategorySelectionLimit\\\":\\\"1\\\",\\\"CategoryCurrentValue\\\":\\\"Peri Peri Spice Level\\\",\\\"CategoryHeader\\\":\\\"Peri Peri Spice Level\\\",\\\"CategoryPrice\\\":\\\"0.000000\\\",\\\"CategoryItems\\\":[{\\\"ItemPrice\\\":\\\"0.000000\\\",\\\"ItemHeader\\\":\\\"Garlic - Simplicity\\\",\\\"ItemSelectionState\\\":\\\"0\\\"},{\\\"ItemPrice\\\":\\\"0.000000\\\",\\\"ItemHeader\\\":\\\"Lemon " 

我在提出API請求時犯了錯誤或錯過了某些內容?我很抱歉,但上面的代碼請求始終有效。

回答

0

除了設置「content-Length」之外,還需要設置內容類型。在你的代碼中嘗試以下內容?

NSString *postLength = [NSString stringWithFormat:@"%d", [postRequestString length]]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; 
[request setValue:@"utf-8" forHTTPHeaderField:@"charset"];