android backendless文件上传

时间:2016-02-25 13:06:02

标签: android api rest android-studio backendless

嗨我尝试将文件上传到我的后端休息api ..但我无法取得好成绩。好像我无法读取文件。代码有什么问题?

Logcat上的错误响应

02-25 21:03:02.138 31815-31815/com.bloxofcode.backendless I/Error: Server reported an error - cannot read the file.

这是示例代码..

public void OnClick_Upload(View v)  {
        // create a file locally so there is something to upload

        String filename = "myhelloworld-async.txt";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(filename);
            fileOutputStream.write("Hello mbaas!\nUploading files is easy!".getBytes());
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        final File file = new File(filename);

        Log.i("FileStream",file.getPath());
        // now upload the file
        backend.Files.upload( file, "/media", new AsyncCallback<BackendlessFile>()
        {
            @Override
            public void handleResponse( BackendlessFile uploadedFile )
            {
                Log.i("Output","File has been uploaded. File URL is - " + uploadedFile.getFileURL());
                //System.out.println( "File has been uploaded. File URL is - " + uploadedFile.getFileURL() );
                //file.delete();
            }

            @Override
            public void handleFault( BackendlessFault backendlessFault )
            {
                Log.i("Error","Server reported an error - " + backendlessFault.getMessage() );
                //System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
            }
        } );
    }

1 个答案:

答案 0 :(得分:0)

看起来您创建的文件无法读取,甚至无法上传文件。

我认为这对你更有效:

byte[] bytes = "Hello mbaas!\nUploading files is easy!".getBytes(); 

Backendless.Files.saveFile( "tempFolder", "myhelloworld-async.txt", bytes, new AsyncCallback<BackendlessFile>()
{
  @Override
  public void handleResponse( BackendlessFile uploadedFile )
  {
       Log.i("Output","File has been uploaded. File URL is - " + uploadedFile.getFileURL());
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i("Error","Server reported an error - " + backendlessFault.getMessage() );
  }
} );