Android Dropbox API v1上传文件并获取此文件的共享链接

时间:2016-12-05 06:42:26

标签: java android dropbox dropbox-api

我上传了文件,当我尝试获取共享链接时,它会给出NullPointerException。

FileInputStream fis = new FileInputStream(mFile);
        String path = mPath + mFile.getName();
        DropboxAPI.Entry response = mApi.putFile(path, fis,
                mFile.length(), null, new ProgressListener() {
                    @Override
                    public long progressInterval() {
                        // Update the progress bar every half-second or so
                        return 500;
                    }

                    @Override
                    public void onProgress(long bytes, long total) {
                        publishProgress(bytes);
                    }
                });
        Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);

        if (response != null) {
            mErrorMsg=response.path;
            Log.e("DbExampleLog", "*****"+response.path+" The uploaded file's rev is: " + response.rev);
            DropboxAPI.DropboxLink shareLink = mApi.share(response.path);
            Log.e("DbExampleLog", "*****"+shareLink+" The uploaded file's rev is: " + response.rev);
            if(shareLink!=null) {
                if(shareLink.url!=null) {
                    Log.e("Null error URL*****",""+shareLink.url);
                    Log.e("Null error URL*****",""+getShareURL(shareLink.url));
                    String shareAddress = getShareURL(shareLink.url).toString();
                    Log.e("DbExampleLog", "URL -" + shareAddress + "*****" + response.path + " The uploaded file's rev is: " + response.rev);
                }
                else
                    Log.e("Null error URL*****",""+shareLink.url);
            }
            else{
                Log.e("Null error*****",""+shareLink);
            }
            return true;
        }

它返回如 -

12-05 12:09:09.207 7335-7502/com.trucker.gtd.satyaki.dropboxintegrationapiv1 E/DbExampleLog: *****/1480919937073.jpg The uploaded file's rev is: 2ef4a7ca38e
12-05 12:09:10.966 7335-7502/com.trucker.gtd.satyaki.dropboxintegrationapiv1 E/DbExampleLog: *****com.dropbox.client2.DropboxAPI$DropboxLink@d9d5da1 The uploaded file's rev is: 2ef4a7ca38e
12-05 12:09:10.966 7335-7502/com.trucker.gtd.satyaki.dropboxintegrationapiv1 E/Null error URL*****: https://db.tt/ru6e39XK0
12-05 12:09:12.457 7335-7502/com.trucker.gtd.satyaki.dropboxintegrationapiv1 E/Null error URL*****: null
12-05 12:09:13.520 7335-7502/com.trucker.gtd.satyaki.dropboxintegrationapiv1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                               Process: com.trucker.gtd.satyaki.dropboxintegrationapiv1, PID: 7335
                                                                                               java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                                   at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                                   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                                   at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                                   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                                   at java.lang.Thread.run(Thread.java:761)
                                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                                                   at com.trucker.gtd.satyaki.dropboxintegrationapiv1.UploadFile.doInBackground(UploadFile.java:147)
                                                                                                   at com.trucker.gtd.satyaki.dropboxintegrationapiv1.UploadFile.doInBackground(UploadFile.java:63)
                                                                                                   at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                                   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                                                   at java.lang.Thread.run(Thread.java:761) 
12-05 12:09:13.645 1248-1248/? E/EGL_emulation: tid 1248: eglCreateSyncKHR(1641): error 0x3004 (EGL_BAD_ATTRIBUTE)
12-05 12:09:13.796 2071-2205/com.android.launcher3 E/EGL_emulation: tid 2205: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-05 12:09:14.006 1530-1640/system_process E/EGL_emulation: tid 1640: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-05 12:09:14.304 7335-7335/com.trucker.gtd.satyaki.dropboxintegrationapiv1 E/WindowManager: android.view.WindowLeaked: Activity com.trucker.gtd.satyaki.dropboxintegrationapiv1.Main has leaked window DecorView@3bf9cac[] that was originally added here
                                                                                                  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:417)
                                                                                                  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)
                                                                                                  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                                                                                                  at android.app.Dialog.show(Dialog.java:316)
                                                                                                  at android.app.AlertDialog$Builder.show(AlertDialog.java:1112)
                                                                                                  at com.dropbox.client2.android.AuthActivity.checkAppBeforeAuth(AuthActivity.java:284)
                                                                                                  at com.dropbox.client2.android.AndroidAuthSession.startAuthentication(AndroidAuthSession.java:213)
                                                                                                  at com.trucker.gtd.satyaki.dropboxintegrationapiv1.Main.onActivityResult(Main.java:132)
                                                                                                  at android.app.Activity.dispatchActivityResult(Activity.java:6915)
                                                                                                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
                                                                                                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
                                                                                                  at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
12-05 12:09:14.310 7335-7335/com.trucker.gtd.satyaki.dropboxintegrationapiv1 E/WindowManager: android.view.WindowLeaked: Activity com.trucker.gtd.satyaki.dropboxintegrationapiv1.Main has leaked window DecorView@e8ad10a[] that was originally added here
                                                                                                  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:417)
                                                                                                  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)
                                                                                                  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                                                                                                  at android.app.Dialog.show(Dialog.java:316)
                                                                                                  at com.trucker.gtd.satyaki.dropboxintegrationapiv1.UploadFile.<init>(UploadFile.java:98)
                                                                                                  at com.trucker.gtd.satyaki.dropboxintegrationapiv1.Main.setLoggedIn(Main.java:144)
                                                                                                  at com.trucker.gtd.satyaki.dropboxintegrationapiv1.Main.onResume(Main.java:168)
                                                                                                  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
                                                                                                  at android.app.Activity.performResume(Activity.java:6766)
                                                                                                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377)
                                                                                                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
                                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1510)
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

主要在这一特定行 -

getShareURL(shareLink.url)

请建议我为什么会出现这种错误,但是昨天使用这个代码就可以了。

Greg提出的更新代码

String getShareURL(String strURL) {
    URLConnection conn = null;
    String redirectedUrl = null;
    try {
        URL inputURL = new URL(strURL);
        conn = inputURL.openConnection();
        conn.connect();

        InputStream is = conn.getInputStream();
        System.out.println("Redirected URL: " + conn.getURL());
        Log.e("Get Redirected URL",""+conn.getURL());
        redirectedUrl = conn.getURL().toString();
        is.close();

    } catch (MalformedURLException e) {
        Log.e("TAG", "Please input a valid URL");
    } catch (IOException ioe) {
        Log.e("TAG", "Can not connect to the URL");
    }

    return redirectedUrl;
}

此代码取自Share file in Dropbox

0 个答案:

没有答案