function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
y1610y1610 

mobile SDKで画像データを取得する方法

お世話になっております。

 

mobile SDKを使用したネイティブアプリケーションから、force.comのメモ&添付の画像データを取得しようとしています。

しかし、データの取得がうまくできずエラーになってしまいます。

以下、こちらで試した内容です。エラーが回避できる方法があれば、ご教示いただきたく思います。

 

1.AttachmentオブジェクトのBodyフィールドを取得(Bodyフィールドに画像データがBase64エンコードされた状態で取得できる予定)

・リクエスト時のコード
    [[SFRestAPI sharedInstance] setApiVersion:@"v24.0"];
    SFRestRequest *request = [[SFRestAPI sharedInstance] requestForRetrieveWithObjectType:@"Attachment" objectId:@"00P10000008NaeJEAS" fieldList:@"Body"];
    [[SFRestAPI sharedInstance] send:request delegate:self];

 

・リクエスト時のログ
SFRestAPI::send: <SFRestRequest 0x73c700
endpoint: /services/data
method: GET
path: /v24.0/sobjects/Attachment/00P10000008NaeJEAS
queryParams: {
  "fields" : "Body"
}

 

・レスポンス取得時のログ
request:didLoadResponse: {
    Body = "/services/data/v24.0/sobjects/Attachment/00P10000008NaeJEAS/Body";
    Id = 00P10000008NaeJEAS;
    attributes =     {
        type = Attachment;
        url = "/services/data/v24.0/sobjects/Attachment/00P10000008NaeJEAS";
    };
}

Bodyにbase64エンコードされたデータを取得できるのを期待していたのですが、
"/services/data/v24.0/sobjects/Attachment/00P10000008NaeJEAS/Body"
というパスを取得しているようです。


2.上記で取得できたパスを指定してリクエスト

・リクエスト時のコード
    [[SFRestAPI sharedInstance] setApiVersion:@"v24.0"];
    SFRestRequest *request = [[SFRestRequest alloc] init];
    [request setEndpoint:@"/services/data"];
    [request setPath: @"/v24.0/sobjects/Attachment/00P10000008NaeJEAS/Body"];
    [[SFRestAPI sharedInstance] send:request delegate:self];

 

・リクエスト時のログ
SFRestAPI::send: <SFRestRequest 0x6e4090
endpoint: /services/data
method: GET
path: /v24.0/sobjects/Attachment/00P10000008NaeJEAS/Body
queryParams: []


・レスポンス時のログ
error parsing json: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x6bbf80 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

というエラーになってしまいます。
おそらく、このリクエストでバイナリデータを受信できていると思われますが、
Mobile SDKのSFRestAPIDelegateのrequest:didLoadResponse:で取得できる値はnullとなってしまい、バイナリデータを取得することができていません。

zhaoxinzhaoxin

Androidで開発した場合、1で取得したURLにより、HttpURLConnectionでダウンロードすることは出来ます。

 

例:

URL url = newURL(ForceApp.getRestClient().getClientInfo().instanceUrl + 1のURL;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); 
conn.setRequestProperty("Authorization", "OAuth " + ForceApp.getRestClient().getAuthToken()); //これは必須だね
InputStream fileStream = conn.getInputStream(); //これはファイルのstreamになります。