- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Dropbox API tutorial 将文件上传到 Dropbox .我在创建 Activity 时登录,并按照教程中的说明向 onResume() 方法添加了一些代码。然后,在单击按钮后,将调用 startService() 方法。
当它到达“com.dropbox.client2.DropboxAPI.Entry response = mDBApi.putFile("/working-draft.txt", inputStream, file.length(), null, null);”行时,应用抛出异常(不是 DropboxException,也不是 DropboxUnlinkedException,......它只是进入 catch Exception block )。
知道哪里出了问题吗?
这是我的代码:
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:
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 服务器进行网络调用以发送文件内容。)您应该改为在后台线程上进行此调用。关于如何执行此操作还有其他答案,例如:How to fix android.os.NetworkOnMainThreadException?
关于android - 在android中将文件上传到Dropbox,putFile方法抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26537338/
我是 React Native 的新手,正在尝试为慈善机构创建一个应用程序。 我在尝试将相机拍摄的图像存储在 Firebase 存储中时遇到问题。 代码片段如下,查看控制台日志,一切看起来都如我所料,
pConnect->SetCurrentDirectory( "C:\\FilesToSendToServer" ); //Need this set on client, i believe cur
Nifi的新手! 我想知道 nifi 是否有办法使用诸如“PutFile”之类的处理器并将其写入一个文件(将数据附加到此文件,或覆盖此文件中的数据)-而不是创建多个不同的文件?是否需要使用另一个处理器
我正在尝试使用 Dropbox API 从 ListView 上传文件。我的代码是: File file = new File(mFileMag.getCurrentDir() + "/" + it
FTP server and client (C windows) 使用 UDP 套接字。 服务器使用 CFtpConnection::PutFile 将文件放在客户端上 一个大文件需要几分钟时间,因
背景 我有一个使用 FetchFile 的工作流处理器、一些中间处理器(例如 RouteOnAttribute 、 HashContent 等),然后是 PutFile处理器。 在整个过程中,内容没有
使用cfftp上传文件时,如何判断文件是否已经存在并重命名? cffile 标签有一个 makeunique 属性,这使得这个过程非常简单。谢谢。 20 年 8 月 2 日更新: 这是我的 cfm。
我尝试使用 Node.js 上传文件,但无法创建目录。请建议我的代码有什么问题: var Client = require('ssh2-sftp-client'); var sftp = new Cl
我正在使用 Cloud Storage for Firebase。我不太清楚如何使用字节数组或使用文件以最快的方式上传图像文件 try { Uri uri = Uri.p
我正在实现 WOPI 服务器以尝试与 Office Online 集成。当 WOPI 客户端调用 PutFile 方法(保存文件)时,它发送一个空请求(req.body.length === 0)。这
我正在使用 Laravel 5.4 并想上传一个文件(我们称之为“lorem.ext”)到存储目录(storage/app/)“path/to/file”,文件名唯一。 为此,我想使用 Storage
我正在开发一个 API,用户可以发送一个文件 url 以将其下载到与用户名相同的特定文件夹。 首先,我向名为 user 的文件系统配置添加了新的磁盘选项,其中包含与经过身份验证的用户用户名同名的特定目
我尝试将本地镜像从手机保存到 Firebase 存储,但收到以下错误。 Permission Denial: reading com.google.android.apps.photos.conten
我尝试将本地镜像从手机保存到 Firebase 存储,但收到以下错误。 Permission Denial: reading com.google.android.apps.photos.conten
string msg = "Writing to file."; byte[] data = System.Text.Encoding.Default.GetBytes(msg); System.IO
以下是我尝试上传文件的两种方式: 1. getURLOfPhoto(assetURL: imagesDictionary[String(whichProfileImage)]! , completio
我正在创建一个程序集文件,以便 get 文件和 put 文件可以作为存储过程从 SQL Server 中调用。下面是放置文件的方法。当我传递 SqlBoolean DeleteLocalFolder=
我是一名优秀的程序员,十分优秀!