2012-12-12 99 views
0

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

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

感谢

回答

0

这是你必须在上传文件时需关注大多是服务器端,客户端,你可以有一个这样的方法(只是有想法,这不是一个全功能的代码):

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(); 
1

您必须试验base64编码文件并发送POST请求到/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的专用堆栈本站资源,例如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
+0

非常感谢您的回复。 – subodh

相关问题