2013-05-11 67 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