【问题标题】:How to upload an audio file in salesforce using android development?如何使用android开发在salesforce中上传音频文件?
【发布时间】:2012-12-12 11:53:32
【问题描述】:

我正在使用 SalesforceMobileSDK-Android 开发一个 android 应用程序。我能够开发一个非常基本的 android 应用程序,在我的应用程序中,我能够从 salesforce 帐户中获取联系人、帐户、潜在客户等详细信息,并对这些数据执行 crud 操作。 在我的 android 应用程序中,我有一个名为 uploadFile 的按钮,现在想通过单击该按钮上传音频文件,但我找不到任何可以帮助我将其上传到 Salesforce 的其他 api从我的 android 客户端应用程序。

如果有任何示例网址或源代码或任何有用的资源,请提供给我。

谢谢

【问题讨论】:

    标签: android api rest salesforce


    【解决方案1】:

    您必须尝试使用​​base64 对文件进行编码并将POSTrequest 发送到/services/data/v26.0/sobjects/attachment/{parent record id}/body 端点。我自己没有做过,但有一些很好的例子:

    1. http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sobject_insert_update_blob.htm - 对 json 消息使用不同的方法。
    2. http://blogs.developerforce.com/developer-relations/2011/09/using-binary-data-with-rest.html - 如果您可以创建服务器端 REST 服务。
    3. 检查 Salesforce 专用 Stack 站点上的资源,例如 https://salesforce.stackexchange.com/questions/1301/image-upload-to-chatter-post
    4. 最后但同样重要的是 - 检查 Salesforce 社区板,例如 http://boards.developerforce.com/t5/APIs-and-Integration/inserting-an-attachment-via-REST/td-p/322699

    【讨论】:

    • 非常感谢您的回复。
    【解决方案2】:

    上传文件时您需要关注的主要是服务器端,客户端您可以采用这样的方法(请理解,这不是完整的功能代码):

     FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
     // open a URL connection to the Servlet
     URL url = new URL(urlString);
     // Open a HTTP connection to the URL
     conn = (HttpURLConnection) url.openConnection();
     // Allow Inputs
     conn.setDoInput(true);
     // Allow Outputs
     conn.setDoOutput(true);
     // Don't use a cached copy.
     conn.setUseCaches(false);
     // Use a post method.
     conn.setRequestMethod("POST");
     conn.setRequestProperty("Connection", "Keep-Alive");
     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
     dos = new DataOutputStream( conn.getOutputStream() );
     dos.writeBytes(twoHyphens + boundary + lineEnd);
     dos.writeBytes("Content-Disposition: form-data; name:\"uploadedfile\";filename=\"" + selectedPath + "\"" + lineEnd);
     dos.writeBytes(lineEnd);
     // create a buffer of maximum size
     bytesAvailable = fileInputStream.available();
     bufferSize = Math.min(bytesAvailable, maxBufferSize);
     buffer = new byte[bufferSize];
     // read file and write it into form...
     bytesRead = fileInputStream.read(buffer, 0, bufferSize);
     while (bytesRead > 0)
     {
      dos.write(buffer, 0, bufferSize);
      bytesAvailable = fileInputStream.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
     }
     // send multipart form data necesssary after file data...
     dos.writeBytes(lineEnd);
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
     // close streams
     Log.e("Debug","File is written");
     fileInputStream.close();
     dos.flush();
     dos.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      相关资源
      最近更新 更多