0

在调用 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 代码。还是有一些不同的格式。

4

1 回答 1

0

在行

NSString *req = [NSString stringWithFormat:@"{\n\"name\":\"%@\",\n\"Body\": \"%@\"\n,\"ParenId\":%@\"\n}",filename,imagedata,receivedrecordid];

JSON 缺少键值"字符。ParentId它应该是:

NSString *req = [NSString stringWithFormat:@"{\n\"name\":\"%@\",\n\"Body\": \"%@\"\n,\"ParenId\":\"%@\"\n}",filename,imagedata,receivedrecordid];

因此 Salesforce webservice 反序列化抛出异常。

于 2013-10-16T05:49:27.980 回答