NSURL * URL = [NSURL URLWithString:@ 「http://www.yourURL.com」];
//NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"{\"createInvoice\":{\"client_id\":\"%@\",\"address\":\"\",\"invoice_title\":\"%@\",\"number\":\"\",\"date\":\"%@\",\"due_date\":\"%@\",\"late_fee\":\"\",\"schedule_date\":\"%@\",\"po_number\":\"\",\"status\":\"paid\",\"notes\":\"invoice notes\",\"terms\":\"invoice terms\",\"items\":[{\"name\":\"Test4\",\"type\":\"Product\",\"description\":\"\",\"unit_cost\":\"15\",\"quantity\":\"100\",\"discount\":\"10\",\"discount_type\":\"Percent\",\"tax1_name\":\"\",\"tax1_type\":\"\",\"tax2_name\":\"\",\"tax2_type\":\"\",\"tax1_percent\":\"\",\"tax2_percent\":\"\"}]}}",selected_Client_ID,invoiceTitleTxtFld.text,invoiveDateTxtFld.text,invoiceDueDateTxtFld.text,scheduleOnTxtFld.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *jsonRequest = [NSString stringWithFormat:@"&json_data=%@",[[NSString stringWithFormat:@"{\"updateInvoice\":{\"invoice_id\":\"%@\",\"client_id\":\"60105\",\"address\":\"ABS\",\"invoice_title\": \"%@\",\"number\":\"inv-7774 new\",\"date\":\"2012-03-30\",\"due_date\":\"2012-04-30\",\"late_fee\":\"\",\"schedule_date\":\"2012-03-31\",\"po_number\":\"PO-7770\",\"status\":\"paid\",\"notes\":\"invoice notes\",\"terms\":\"invoice terms\",\"items\":[{\"name\":\"Test4\",\"type\":\"Product\",\"description\":\"Test4 desc\",\"unit_cost\":\"15\",\"quantity\":\"100\",\"discount\":\"10\",\"discount_type\":\"Percent\",\"tax1_name\":\"\",\"tax1_type\":\"\",\"tax2_name\":\"\",\"tax2_type\":\"\",\"tax1_percent\":\"\",\"tax2_percent\":\"\"}]}}",delegate.invoice_ID,invoiceTitleTxtFld.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];
NSLog(@"%@",json_data);
//[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [json_data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[[jsonRequest stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES]];
// [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
NSUrlsConnection conn_InvEdit * = [[NSURLConnection的頁頭] initWithRequest:請求 委託:自我];
// Successful connection.
if (conn_InvEdit) {
// [self initSpinner];
// [self spinBegin];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData=data;
[data release];
}
// Unsuccessful connection.
else {
}
// Clean up
[url release];
[request release];
// Close keypad.
Try with this code, If you still have any problem then send me your complete question at [email protected] I'll send you sample app with solution.
在xml中有沒有à或Ã? – 2012-04-15 10:21:23
'utfstring'不是UTF8String字符串,它只是一個8位字符串,可能是內部'kCFStringEncodingMacRoman'格式,它的編碼範圍爲129-255。在unicode中,這些字符將由多個(本例中爲兩個)8位代碼組成, – zaph 2012-04-15 10:44:06