本文介绍了如何使用 Salesforce Mobile SDK for android 上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在原生 Android 应用程序中使用 Salesforce SDK (4.1.x).我使用 RestClient.sendAsync 方法将我的表单数据发布到自定义对象.那部分工作正常.现在我需要上传并附上移动用户拍摄的照片.我看到 RestClient 有一个 uploadFile 方法.这是正确的方法吗?如果是这样,那么我如何将上传的文件连接到自定义表单数据?

I am using the Salesforce SDK (4.1.x) in a native Android app. I use the RestClient.sendAsync method to post my form data to a custom object. That part is working fine. Now I need to upload and attach a photo that was taken by the mobile user. I see that RestClient has an uploadFile method. Is this the correct method? If so then how do I connect the uploaded file to the custom form data?

推荐答案

好的.我想通了.首先,使用以下内容创建父对象(主表单数据).

Ok. I figured this out. First, create the parent object (the main form data) using the following.

request = RestRequest.getRequestForCreate(apiVersion, objectType, fields);
client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {...

在 onSuccess 方法中,您将从响应中获取新对象的 id.有很多示例展示了如何获取 JSON 对象和 id.有了这个 parentId,我们现在可以创建附件.代码看起来像这样.

In the onSuccess method you will get the id of the new object from the response. There are plenty of examples that show how to get the JSON object and the id. Armed with this parentId we can now create the attachment. The code looks something like this.

private void postImageAsAttachment(String parentId, String title) {
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put("Name", title);
    fields.put("ParentId", parentId);
    fields.put("Body", ImageHelper.getBase64FromImage(mCurrentPhotoPath));

    RestRequest request = null;
    try {
        request = RestRequest.getRequestForCreate(apiVersion, "Attachment", fields);
    } catch (Exception ex) {
        Log.d(TAG, "sendRequest: ", ex);
        Toast.makeText(MainActivity.this, "The file upload failed: " + ex.toString(), Toast.LENGTH_LONG).show();
    }
    client.sendAsync(request, new RestClient.AsyncRequestCallback() {...

我正在使用一个名为 ImageHelper 的简单类,它只加载图像文件、执行图像压缩(如有必要),并对图像数据进行 base64 编码.结果是创建了一个附件"对象作为父对象的子对象.

I'm using a simple class called ImageHelper that simply loads the image file, performs image compression (if necessary), and base64 encodes the image data. The result is that an "Attachment" object is created as a child of the parent object.

我希望这能帮助下一个人.

I hope this helps the next person.

这篇关于如何使用 Salesforce Mobile SDK for android 上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:54