@try {
NSURL *postURL = [NSURL URLWithString: @"http://yyy.com/api/Customer/"];
NSDictionary *myDic = [[NSDictionary alloc] initWithObjectsAndKeys:
self.firstNameTextField.text, CustFirstName,
self.lastNameTextField.text, CustLastName,
nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDic options:NSJSONWritingPrettyPrinted error:&error];
if (!jsonData) {
NSLog(@"Got an error: %@", error);
} else {
// Get our Access Token back
NSString *savedToken = [[NSUserDefaults standardUserDefaults]
stringForKey:@"savedToken"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:postURL];
[request setHTTPMethod: @"POST"];
[request setValue: @"application/json" forHTTPHeaderField: @"Accept"];
[request setValue: @"application/json; charset=utf-8" forHTTPHeaderField: @"content-type"];
[request setValue:savedToken forHTTPHeaderField:@"Authorization"];
[request setHTTPBody:jsonData];
NSHTTPURLResponse *response = nil;
NSLog(@"Response code: %ld", (long)[response statusCode]);
//NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSLog(@"Saving Success!");
[self performSegueWithIdentifier:@"edit_add_customer" sender:self];
} else {
NSLog(@"Saving Fail!");
[self alertStatus:@"Please try again" :@"Saving Fail" :0];
}
}
}
@catch (NSException *exception) {
NSLog(@"Exception: %@", exception);
[self alertStatus:@"Error Retrieving Customer Data." :@"Error!" :0];
}
反正我的JSON字符串是正確的,正如我在休息客戶所空響應。它能夠保存。 我嘗試記錄我的響應StatusCode,它顯示爲'0'。有人可以幫我弄這個嗎?
你想念的連接請求後檢查的StatusCode:NSURLConnection的*連接= [[NSURLConnection的alloc] initWithRequest:request delegate:self]; ? – Horst 2014-10-09 08:33:35