上传文件到Box.com(Android)

时间:2015-11-14 02:32:05

标签: java android box

我一直关注https://github.com/box/box-android-sdk/上的示例代码以及文档。但是,每当我尝试将文件上传到box.com时,我都会收到以下错误:

  

11-13 18:25:45.222 20996-21555 / com.caa.capturebox W / System.err:com.box.androidsdk.content.BoxException:发送请求时发生错误(401)   11-13 18:25:45.237 20996-21555 / com.caa.capturebox W / System.err:at com.box.androidsdk.content.requests.BoxRequestUpload.send(BoxRequestUpload.java:114)   11-13 18:25:45.238 20996-21555 / com.caa.capturebox W / System.err:at com.caa.capturebox.MainActivity $ 5.run(MainActivity.java:441)

以下是我最后一次尝试,但无论是尝试从输入流还是从本地文件上传,我都会遇到相同的错误。

    private void uploadFile() {
    mDialog = ProgressDialog.show(MainActivity.this, getText(R.string.boxsdk_Please_wait), getText(R.string.boxsdk_Please_wait));
    new Thread() {
        @Override
        public void run() {
            try {
                Log.e("mya", "uploadFile: entered try");
                FileInputStream uploadStream = new FileInputStream(image_name);
                //InputStream uploadStream = getResources().getAssets().open(uploadFileName);
                String destinationFolderId = "0";
                String uploadName = "BoxSDKUpload.jpg";

                //BoxRequestsFile.UploadFile request = mFileApi.getUploadRequest(uploadStream, uploadName, destinationFolderId);
                BoxRequestsFile.UploadFile request = mFileApi.getUploadRequest(image_name, "0");
                final BoxFile uploadFileInfo = request.send();
                showToast("Uploaded " + uploadFileInfo.getName());
                loadRootFolder();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (BoxException e) {
                e.printStackTrace();
                BoxError error = e.getAsBoxError();
                if (error != null && error.getStatus() == HttpStatus.SC_CONFLICT) {
                    ArrayList<BoxEntity> conflicts = error.getContextInfo().getConflicts();
                    if (conflicts != null && conflicts.size() == 1 && conflicts.get(0) instanceof BoxFile) {
                        //uploadNewVersion((BoxFile) conflicts.get(0));
                        return;
                    }
                }
                showToast("Upload failed");
            } finally {
                mDialog.dismiss();
            }
        }
    }.start();
}

1 个答案:

答案 0 :(得分:0)

因为您有(401)响应,我认为它是未经授权的http代码返回。您是否已登录框:box login documentation

请使用此代码登录以使用包装服务:

BoxSession session = new BoxSession(context);
session.authenticate();