2012-12-12 48 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

相關問題