在调用 apexrest webservice 以通过调用方法将附件上传到特定记录。所以为此我对 Json 进行了硬编码。
-(void)uploadToSalesforce
{
NSData *imagedata = UIImageJPEGRepresentation(imagePreview.image, 1.0);
int datalength = [imagedata length];
NSString *filename = [NSString stringWithFormat:@"Supload_iPhone_%d.jpg",datalength];
NSString *req = [NSString stringWithFormat:@"{\n\"name\":\"%@\",\n\"Body\": \"%@\"\n,\"ParenId\":%@\"\n}",filename,imagedata,receivedrecordid];
const char *utfString = [req UTF8String];
NSData *postData = [NSData dataWithBytes:utfString length:strlen(utfString)];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *requestUrl = [[NSMutableURLRequest alloc] init ];
[requestUrl setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/services/apexrest/Account/",receivedinstanceurl]]];
[requestUrl setHTTPMethod:@"POST"];
[requestUrl setValue:postLength forHTTPHeaderField:@"Content-length"];
[requestUrl setValue:[NSString stringWithFormat:@"Bearer %@",receivedaccesstoken] forHTTPHeaderField:@"Authorization"];
[requestUrl setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestUrl setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *reponseData = [NSURLConnection sendSynchronousRequest:requestUrl returningResponse:&response error:&err];
NSString *res = [[NSString alloc] initWithData:reponseData encoding:NSASCIIStringEncoding];
}
作为回应,它说有
[{"message":"Unexpected parameter encountered during deserialization: Name at [line:2, column:9]","errorCode":"JSON_PARSER_ERROR"}]
在控制台 JSON 似乎正确但无法解析参数“名称”。我认为这不是 IOS 代码。还是有一些不同的格式。