将文件上传到android中的Dropbox,putFile方法抛出异常 [英] Uploading file to Dropbox in android, putFile method throws Exception

查看:97
本文介绍了将文件上传到android中的Dropbox,putFile方法抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Dropbox API教程。我在创建活动时登录,并按照本教程中的说明向onResume()方法添加了一些代码。然后,单击按钮后,调用startService()方法。

I'm trying to upload a file to Dropbox using the Dropbox API tutorial. I logged in when creating the Activity and I add some code to the onResume() method as said in the tutorial. Then, after clicking a button, startService() method is called.

到达行 com.dropbox.client2.DropboxAPI.Entry response = mDBApi.putFile( /working-draft.txt,inputStream,file.length(),null,null);,应用程序将引发一个异常(不是DropboxException或DropboxUnlinkedException,...。它只是进入catch Exception块)。

When it reaches the line "com.dropbox.client2.DropboxAPI.Entry response = mDBApi.putFile("/working-draft.txt", inputStream, file.length(), null, null);", the app throws an Exception (not a DropboxException, nor DropboxUnlinkedException, .... It just enters the catch Exception block).

任何想法可能出什么问题吗?

Any idea what might be wrong?

这是我的代码:

private final static String APP_KEY = "xxxxxxxxxxxxx";
private final static String APP_SECRET = "xxxxxxxxxxxxx";
private DropboxAPI<AndroidAuthSession> mDBApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);   
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    AndroidAuthSession s = mDBApi.getSession();
    s.startOAuth2Authentication(MainActivity.this);
}

@Override
protected void onResume() {
    super.onResume();
    if (mDBApi.getSession().authenticationSuccessful()) {
        try {
            // Required to complete auth, sets the access token on the session
            mDBApi.getSession().finishAuthentication();

            String accessToken = mDBApi.getSession().getOAuth2AccessToken();
        } catch (IllegalStateException e) {
            Log.i("DbAuthLog", "Error authenticating", e);
        }
    }

}

public void startService(View v) {    

    String text = "Hello world";
    String filePath = Environment.getExternalStorageDirectory() + "/working-draft.txt";
    Log.i("SERVICE", filePath);
    File file = new File(filePath);
    try {
        if(!file.exists()){
            file.createNewFile();
        }
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.close();
        FileInputStream inputStream = new FileInputStream(file);
        com.dropbox.client2.DropboxAPI.Entry response = mDBApi.putFile("/working-draft.txt", inputStream, file.length(), null, null);
        Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);
    }
    catch(DropboxUnlinkedException e){
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        Log.i("Exception", errors.toString());
    }
    catch(DropboxFileSizeException e){
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        Log.i("Exception", errors.toString());
    }
    catch(DropboxServerException e){
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        Log.i("Exception", errors.toString());
    }
    catch(DropboxIOException e){
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        Log.i("Exception", errors.toString());
    }
    catch (DropboxException e){
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        Log.i("Exception", errors.toString());
    }
    catch (Exception e) {
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        Log.i("Exception", errors.toString());
    }
}

这是我得到的stackTrace:

This is the stackTrace I got:

10-23 22:42:34.254: I/Exception(28082): android.os.NetworkOnMainThreadException
10-23 22:42:34.254: I/Exception(28082):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
10-23 22:42:34.254: I/Exception(28082):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-23 22:42:34.254: I/Exception(28082):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-23 22:42:34.254: I/Exception(28082):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-23 22:42:34.254: I/Exception(28082):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-23 22:42:34.254: I/Exception(28082):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-23 22:42:34.254: I/Exception(28082):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-23 22:42:34.254: I/Exception(28082):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-23 22:42:34.254: I/Exception(28082):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-23 22:42:34.254: I/Exception(28082):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-23 22:42:34.254: I/Exception(28082):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-23 22:42:34.254: I/Exception(28082):     at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:387)
10-23 22:42:34.254: I/Exception(28082):     at com.dropbox.client2.DropboxAPI$BasicUploadRequest.upload(DropboxAPI.java:1119)
10-23 22:42:34.254: I/Exception(28082):     at com.dropbox.client2.DropboxAPI.putFile(DropboxAPI.java:1460)
10-23 22:42:34.254: I/Exception(28082):     at com.example.example.MainActivity.startService(MainActivity.java:81)
10-23 22:42:34.254: I/Exception(28082):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 22:42:34.254: I/Exception(28082):     at java.lang.reflect.Method.invoke(Method.java:525)
10-23 22:42:34.254: I/Exception(28082):     at android.view.View$1.onClick(View.java:3628)
10-23 22:42:34.254: I/Exception(28082):     at android.view.View.performClick(View.java:4240)
10-23 22:42:34.254: I/Exception(28082):     at android.view.View$PerformClick.run(View.java:17721)
10-23 22:42:34.254: I/Exception(28082):     at android.os.Handler.handleCallback(Handler.java:730)
10-23 22:42:34.254: I/Exception(28082):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 22:42:34.254: I/Exception(28082):     at android.os.Looper.loop(Looper.java:137)
10-23 22:42:34.254: I/Exception(28082):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-23 22:42:34.254: I/Exception(28082):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 22:42:34.254: I/Exception(28082):     at java.lang.reflect.Method.invoke(Method.java:525)
10-23 22:42:34.254: I/Exception(28082):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-23 22:42:34.254: I/Exception(28082):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-23 22:42:34.254: I/Exception(28082):     at dalvik.system.NativeStart.main(Native Method)

谢谢。

推荐答案

您将收到 NetworkOnMainThreadException ,这意味着您正在尝试在主线程上进行网络调用,而Android上是不允许的。 ( putFile 方法对Dropbox API服务器进行网络调用以发送文件内容。)您应该在后台线程上进行此调用。还有其他有关如何执行此操作的答案,例如:如何修复android.os.NetworkOnMainThreadException?

You're getting a NetworkOnMainThreadException, which means you're trying to make a network call on the main thread, which isn't allowed on Android. (The putFile method makes a network call to the Dropbox API servers to send up the file content.) You should make this call on a background thread instead. There are other answers about how to do this, e.g.: How to fix android.os.NetworkOnMainThreadException?

这篇关于将文件上传到android中的Dropbox,putFile方法抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆