2013-05-11 48 views
0

我開發了使用Android Parse API上傳文件的代碼。Android - Parse.com上傳文件問題

我的代碼如下:

String name = "" + IMGname; 
ParseFile file = new ParseFile(name.toLowerCase(), byteArray); 
String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT); 

ParseObject gameScore = new ParseObject("UserDetails_New"); 
gameScore.put("userName", "" + userName.getText().toString().toLowerCase()); 

gameScore.put("userPhotoProfile", file); 

gameScore.saveInBackground(); 

我的問題是:在上面的代碼工作的大部分時間,但不是每一次。有時它會上傳文件,有時不會。任何人都可以告訴我爲什麼發生這種情況?

回答

2

我已經解決了這個使用:file.saveInBackground();

這意味着我們必須首先使用下面的代碼上傳文件:

byte[] data = "Working at Parse is great!".getBytes(); 
ParseFile file = new ParseFile("resume.txt", data); 
file.saveInBackground(); 

保存完成後,執行以下步驟

ParseObject jobApplication = new ParseObject("JobApplication"); 
jobApplication.put("applicantName", "Joe Smith"); 
jobApplication.put("applicantResumeFile", file); 
jobApplication.saveInBackground(); 

所以,我的回答是:

String name = "" + IMGname; 
       ParseFile file = new ParseFile(name.toLowerCase(), byteArray); 
       file.saveInBackground(); 

       ParseObject gameScore = new ParseObject("UserDetails_New"); 
       gameScore.put("userName", "" + userName.getText().toString().toLowerCase()); 

       gameScore.put("userPhotoProfile", file); 

       gameScore.saveInBackground(); 
1
gameScore.saveInBackground(); 

而不是嘗試偵聽結果回調。這樣你就可以知道文件保存過程出了什麼問題。

gameScore.saveInBackground(new SaveCallback() { 

    @Override 
    public void done(ParseException arg0) { 
     if(arg0 != null) 
     { 
      //Handle Error here 
     } 
    } 
}); 

您也可以使用saveEventually方法。這種方法的優點是,它會在網絡連接回來時嘗試更新對象,並且如果您在保存過程之間關閉應用程序,Parse框架將保持此狀態,並將在嘗試保存對象時用戶返回到應用程序。

+0

你的代碼沒有幫助。我已經試過這個.. – 2013-05-11 06:40:21

+0

我不是聲稱它是一個解決方案,它是一個提示,糾正在服務器端出現錯誤,你可以找出錯誤或路由的原因......還有一件事在解析一個普通賬戶時,每秒nof API請求是20,這也可能是一個問題。仍然是一個假設。 – Triode 2013-05-11 06:43:54