Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GoogleDrive insert.execute()引发IOException-如何获取有关错误的更多信息?_Android_Google Drive Api - Fatal编程技术网

Android GoogleDrive insert.execute()引发IOException-如何获取有关错误的更多信息?

Android GoogleDrive insert.execute()引发IOException-如何获取有关错误的更多信息?,android,google-drive-api,Android,Google Drive Api,我正试图上传一个文件到我的GoogleDrive,但我遇到了一个IOException,我找不到原因?怎么了?应该修改什么? 凭据、文件路径、文件名、文件类型(文本/csv)正确。。。父id folderID很好。。。 很明显我以前测试过如果连接到互联网。。。但没有办法,我总是在第行得到这个IOException:file=insert.execute()(我执行了一个调试器运行…) 父ID“mFolderID”为空。。(这让消息有了意义,没有任何id…) 我修改了我的设计。。。以前使用2个线程

我正试图上传一个文件到我的GoogleDrive,但我遇到了一个IOException,我找不到原因?怎么了?应该修改什么? 凭据、文件路径、文件名、文件类型(文本/csv)正确。。。父id folderID很好。。。 很明显我以前测试过如果连接到互联网。。。但没有办法,我总是在第行得到这个IOException:file=insert.execute()(我执行了一个调试器运行…)


父ID“mFolderID”为空。。(这让消息有了意义,没有任何id…) 我修改了我的设计。。。以前使用2个线程和WeakReference。。。一个用于获取folderID(并设置私有静态字符串mFolderId),另一个用于上载(使用mFolderId)。。。但是使用WeakReference,这个新线程的mFolderId被设置为null

现在我只使用一个线程来获取folderID并上传其中的文件

    private void uploadFileInSwinglogFolder() {
    mUploadFileInSwinglogFolderThread = new UploadFileInSwinglogFolderThread(UploadActivity.this);
    mUploadFileInSwinglogFolderThread.start();      
}

// static inner classes don't hold implicit references to their
// enclosing class, so the Activity instance won't be leaked across configuration change    
private static class UploadFileInSwinglogFolderThread extends Thread {
    private final WeakReference<UploadActivity> mActivity;

    public UploadFileInSwinglogFolderThread(UploadActivity activity) {
      mActivity = new WeakReference<UploadActivity>(activity);
    }

    private boolean mRunning = false;
    @Override
    public void run() {
        UploadActivity activity = mActivity.get();
        mRunning = true;
        while (mRunning) {
            mResultList = new ArrayList<File>();
            mIOException = false;
            mFolderId = null;
            Files f1 = mService.files();
            Files.List request = null;
                try  {
                    request = f1.list();
                    // searching for named folder at root level
                    String aQuery = "'root' in parents and mimeType='application/vnd.google-apps.folder' and title='"+ mFolderName + "'";
                    request.setQ(aQuery);                       
                    FileList fileList = request.execute();
                    mResultList.addAll(fileList.getItems());
                    request.setPageToken(fileList.getNextPageToken());
                } catch (UserRecoverableAuthIOException e) {
                    activity.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                } catch (IOException e) {
                    Log.e(TAG, "STACKTRACE");
                    Log.e(TAG, Log.getStackTraceString(e));
                    mIOException = true;
                    Message msg = activity.mHandler.obtainMessage();
                    Bundle bundle = new Bundle();
                    bundle.putInt("msgKey", UploadActivity.IO_EXCEPTION);
                    msg.setData(bundle);
                    activity.mHandler.sendMessage(msg);
                } finally {
                    if (mResultList.size() == 0) {
                        Message msg = activity.mHandler.obtainMessage();
                        Bundle bundle = new Bundle();
                        bundle.putInt("msgKey", UploadActivity.NO_SWIMLOGS_FOLDER);
                        msg.setData(bundle);
                        activity.mHandler.sendMessage(msg);
                    } else {
                        // Got the parent folderID in googleDrive
                        mFolderId =  mResultList.get(0).getId();
                        File file = null;
                        try {
                            java.io.File fileContent = new java.io.File(mFileUri.getPath());
                            //FileContent mediaContent = new FileContent("text/csv", fileContent);
                            InputStreamContent mediaContent = new InputStreamContent("text/csv", new BufferedInputStream(new FileInputStream(fileContent)));
                            mediaContent.setLength(fileContent.length());

                            File body = new File();
                            body.setTitle(fileContent.getName());                   
                            body.setMimeType("text/csv");
                            Log.d(TAG, "mFolderID: " + mFolderId);
                            body.setParents(Arrays.asList(new ParentReference().setId(mFolderId)));

                            Drive.Files.Insert insert = mService.files().insert(body, mediaContent);

                            MediaHttpUploader uploader = insert.getMediaHttpUploader();
                            uploader.setDirectUploadEnabled(false);
                            uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);

我现在可以获得堆栈跟踪,添加Log.e(TAG,Log.getStackTraceString(e));但是,正如错误经常指出的那样:{“代码”:404,“错误”:[{“域”:“全局”,“消息”:“未找到文件:”,“原因”:“未找到”}],“消息”:“未找到文件:”}根本不提供信息。。。Google API文档:建议的操作:向用户报告他们没有对该文件的读取权限或该文件不存在。告诉他们,他们应该向文件所有者请求对该文件的许可。。。哪个文件?我正在上载的现有文件夹还是现有的父文件夹?
    private void uploadFileInSwinglogFolder() {
    mUploadFileInSwinglogFolderThread = new UploadFileInSwinglogFolderThread(UploadActivity.this);
    mUploadFileInSwinglogFolderThread.start();      
}

// static inner classes don't hold implicit references to their
// enclosing class, so the Activity instance won't be leaked across configuration change    
private static class UploadFileInSwinglogFolderThread extends Thread {
    private final WeakReference<UploadActivity> mActivity;

    public UploadFileInSwinglogFolderThread(UploadActivity activity) {
      mActivity = new WeakReference<UploadActivity>(activity);
    }

    private boolean mRunning = false;
    @Override
    public void run() {
        UploadActivity activity = mActivity.get();
        mRunning = true;
        while (mRunning) {
            mResultList = new ArrayList<File>();
            mIOException = false;
            mFolderId = null;
            Files f1 = mService.files();
            Files.List request = null;
                try  {
                    request = f1.list();
                    // searching for named folder at root level
                    String aQuery = "'root' in parents and mimeType='application/vnd.google-apps.folder' and title='"+ mFolderName + "'";
                    request.setQ(aQuery);                       
                    FileList fileList = request.execute();
                    mResultList.addAll(fileList.getItems());
                    request.setPageToken(fileList.getNextPageToken());
                } catch (UserRecoverableAuthIOException e) {
                    activity.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                } catch (IOException e) {
                    Log.e(TAG, "STACKTRACE");
                    Log.e(TAG, Log.getStackTraceString(e));
                    mIOException = true;
                    Message msg = activity.mHandler.obtainMessage();
                    Bundle bundle = new Bundle();
                    bundle.putInt("msgKey", UploadActivity.IO_EXCEPTION);
                    msg.setData(bundle);
                    activity.mHandler.sendMessage(msg);
                } finally {
                    if (mResultList.size() == 0) {
                        Message msg = activity.mHandler.obtainMessage();
                        Bundle bundle = new Bundle();
                        bundle.putInt("msgKey", UploadActivity.NO_SWIMLOGS_FOLDER);
                        msg.setData(bundle);
                        activity.mHandler.sendMessage(msg);
                    } else {
                        // Got the parent folderID in googleDrive
                        mFolderId =  mResultList.get(0).getId();
                        File file = null;
                        try {
                            java.io.File fileContent = new java.io.File(mFileUri.getPath());
                            //FileContent mediaContent = new FileContent("text/csv", fileContent);
                            InputStreamContent mediaContent = new InputStreamContent("text/csv", new BufferedInputStream(new FileInputStream(fileContent)));
                            mediaContent.setLength(fileContent.length());

                            File body = new File();
                            body.setTitle(fileContent.getName());                   
                            body.setMimeType("text/csv");
                            Log.d(TAG, "mFolderID: " + mFolderId);
                            body.setParents(Arrays.asList(new ParentReference().setId(mFolderId)));

                            Drive.Files.Insert insert = mService.files().insert(body, mediaContent);

                            MediaHttpUploader uploader = insert.getMediaHttpUploader();
                            uploader.setDirectUploadEnabled(false);
                            uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
                            file = insert.execute();                                
                        } catch (IOException e) {
                            Log.d(TAG, "IOException in uploading file code: ");
                            Log.e(TAG, "STACKTRACE");
                            Log.e(TAG, Log.getStackTraceString(e));
                            Message msg = activity.mHandler.obtainMessage();
                            Bundle bundle = new Bundle();
                            bundle.putInt("msgKey", UploadActivity.IO_EXCEPTION);
                            msg.setData(bundle);
                            activity.mHandler.sendMessage(msg);
                        } finally {
                            Log.d(TAG, "end try - finally block ");
                            if ( file != null) {
                                Log.d(TAG, "Uploading file done: " + file.getTitle());
                                Log.d(TAG, "UploadActivity - thread - file uploaded");
                                Message msg = activity.mHandler.obtainMessage();
                                Bundle bundle = new Bundle();
                                bundle.putInt("msgKey", UploadActivity.SWIMLOG_UPLOADED);
                                msg.setData(bundle);
                                activity.mHandler.sendMessage(msg);                                 
                            }
                        }
                    }
                    mRunning = false;
                }
        }
    }
    public void close() {
        mRunning = false;
    }
}