- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Android 开发的新手。如果我需要添加更多详细信息,请告诉我。
据我所知,AsyncTask 使用的是工作线程,因此它不应导致 NetworkOnMainThreadException。我做错了什么吗?谢谢。
我已经暗示了OneDrive's Explorer的结构例子。我创建了一个扩展应用程序的 baseApplication,其中包括一个 IOneDriveClient 实例。
/**
* Base application
*/
public class OneDriveBaseApplication extends Application {
public static final String Log_Tag = "MyDebug";
/**
* The service instance
*/
private final AtomicReference<IOneDriveClient> mClient = new AtomicReference<>();
/**
* Create the client configuration
* @return the newly created configuration
*/
private IClientConfig createConfig() {
final MSAAuthenticator msaAuthenticator = new MSAAuthenticator() {
@Override
public String getClientId() {
SharedPreferences onedrivePref = getApplicationContext().getSharedPreferences(getString(R.string.onedrive_pref_key), Context.MODE_PRIVATE);
String msa_client_id = onedrivePref.getString(getString(R.string.onedrive_pref_app_key), "");
Log.v(Log_Tag, msa_client_id);
return msa_client_id;
}
@Override
public String[] getScopes() {
return new String[]{"onedrive.appfolder offline_access"};
}
};
final IClientConfig config = DefaultClientConfig.createWithAuthenticator(msaAuthenticator);
config.getLogger().setLoggingLevel(LoggerLevel.Debug);
return config;
}
/**
* Used to setup the Services
*
* @param activity the current activity
* @param serviceCreated the callback
*/
synchronized void createOneDriveClient(final Activity activity, final ICallback<Void> serviceCreated) {
final DefaultCallback<IOneDriveClient> callback = new DefaultCallback<IOneDriveClient>(activity) {
@Override
public void success(final IOneDriveClient result) {
mClient.set(result);
serviceCreated.success(null);
}
@Override
public void failure(final ClientException error) {
serviceCreated.failure(error);
}
};
new OneDriveClient
.Builder()
.fromConfig(createConfig())
.loginAndBuildClient(activity, callback);
}
/**
* Get an instance of the service
*
* @return The Service
*/
synchronized IOneDriveClient getOneDriveClient() {
if (mClient.get() == null) {
throw new UnsupportedOperationException("Unable to generate a new service object");
}
return mClient.get();
}
}
在我的 fragment 中,我创建了一个异步任务类来下载文件。
private class DownloadTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
final OneDriveBaseApplication app = (OneDriveBaseApplication)getActivity().getApplication();
try {
app.getOneDriveClient()
.getDrive()
.getSpecial("approot")
.getChildren()
.buildRequest()
.get(new DefaultCallback<IItemCollectionPage>(getActivity()) {
@Override
public void success(final IItemCollectionPage result) {
Log.v(OneDriveBaseApplication.Log_Tag, "get children success");
if (result != null) {
for (final Item childItem : result.getCurrentPage()) {
try {
final String itemId = childItem.id;
final String itemName = childItem.name;
byte[] buffer = new byte[1024];
int len = 0;
Log.v(OneDriveBaseApplication.Log_Tag, "name " + itemName + " id " + itemId);
final File firDownloadDir = new File(Environment.getExternalStorageDirectory() + File.separator + "downloaded_files");
firDownloadDir.mkdir();
final File file = new File(firDownloadDir, itemName);
Log.v(OneDriveBaseApplication.Log_Tag, "File path " + file.getPath());
FileOutputStream out = new FileOutputStream(file);
Log.v(OneDriveBaseApplication.Log_Tag, "FileOutputStream Established");
/* Get the file from OneDrive*/
Log.v(OneDriveBaseApplication.Log_Tag, "To get the file from OneDrive");
InputStream in = app.getOneDriveClient()
.getDrive()
.getItems(itemId)
.getContent()
.buildRequest()
.get();
Log.v(OneDriveBaseApplication.Log_Tag, "Get the file from OneDrive succesfully");
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
Log.v(OneDriveBaseApplication.Log_Tag, "Write successfully: " + childItem.name);
} catch (FileNotFoundException ex) {
Log.v(OneDriveBaseApplication.Log_Tag, "File not found when downloading: " + childItem.name);
} catch (IOException ex) {
Log.v(OneDriveBaseApplication.Log_Tag, "IOException when writing/reading: " + childItem.name);
}
}
}
}
@Override
public void failure(ClientException ex) {
Log.v(OneDriveBaseApplication.Log_Tag, "ClientException");
}
});
} catch (UnsupportedOperationException ex) {
Log.v(OneDriveBaseApplication.Log_Tag, "UnsupportedOperationException");
} catch (Error error) {
Log.v(OneDriveBaseApplication.Log_Tag, "Error for whatsoever");
}
return null;
}
}
并执行它
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute();
但是,在发送 GET 请求时会导致 NetworkOnMainThreadException。
202: Starting to send request, URL https://api.onedrive.com/v1.0/drive/items/AB76E7297xxxxxxx!110/content
206: Request Method GET
312: Error during http request
com.onedrive.sdk.core.ClientException: Error during http request
at com.onedrive.sdk.http.DefaultHttpProvider.sendRequestInternal(DefaultHttpProvider.java:309)
at com.onedrive.sdk.http.DefaultHttpProvider.send(DefaultHttpProvider.java:165)
at com.onedrive.sdk.http.BaseStreamRequest.send(BaseStreamRequest.java:76)
at com.onedrive.sdk.generated.BaseItemStreamRequest.get(BaseItemStreamRequest.java:60)
at tw.com.www.onedrivedemo.PlaceholderFragment$DownloadTask$1.success(PlaceholderFragment.java:159)
at tw.com.www.onedrivedemo.PlaceholderFragment$DownloadTask$1.success(PlaceholderFragment.java:133)
at com.onedrive.sdk.concurrency.DefaultExecutors$1.run(DefaultExecutors.java:88)
at com.onedrive.sdk.concurrency.SynchronousExecutor$1.onPostExecute(SynchronousExecutor.java:54)
at com.onedrive.sdk.concurrency.SynchronousExecutor$1.onPostExecute(SynchronousExecutor.java:46)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
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)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:300)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:808)
at com.android.okhttp.okio.Okio$1.write(Okio.java:76)
at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at com.android.okhttp.okio.RealBufferedSink.flush(RealBufferedSink.java:221)
at com.android.okhttp.internal.http.HttpConnection.flush(HttpConnection.java:141)
at com.android.okhttp.internal.http.HttpTransport.finishRequest(HttpTransport.java:52)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:904)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:782)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:521)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java)
at com.onedrive.sdk.http.UrlConnection.getResponseCode(UrlConnection.java:100)
at com.onedrive.sdk.http.DefaultHttpProvider.sendRequestInternal(DefaultHttpProvider.java:245)
at com.onedrive.sdk.http.DefaultHttpProvider.send(DefaultHttpProvider.java:165)
at com.onedrive.sdk.http.BaseStreamRequest.send(BaseStreamRequest.java:76)
at com.onedrive.sdk.generated.BaseItemStreamRequest.get(BaseItemStreamRequest.java:60)
at tw.com.www.onedrivedemo.PlaceholderFragment$DownloadTask$1.success(PlaceholderFragment.java:159)
at tw.com.www.onedrivedemo.PlaceholderFragment$DownloadTask$1.success(PlaceholderFragment.java:133)
at com.onedrive.sdk.concurrency.DefaultExecutors$1.run(DefaultExecutors.java:88)
at com.onedrive.sdk.concurrency.SynchronousExecutor$1.onPostExecute(SynchronousExecutor.java:54)
at com.onedrive.sdk.concurrency.SynchronousExecutor$1.onPostExecute(SynchronousExecutor.java:46)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
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)
最佳答案
看起来 OneDriveClient
正在为您处理异步。
因此您不需要在 AsyncTask
中调用本质上是请求设置的内容。您可能希望在 success()
回调中放置一个 AsyncTask
,因为问题出在您在客户端调用 .get()
时。这是一个阻塞操作,它将在当前线程上执行操作。在这种情况下,该线程是从该线程调用的 success
的 mainThread。
解决方案:了解哪些部分是异步的并将它们从主线程中移出。简化您的代码,以便您可以了解失败的部分。
使用断点和/或日志来查看您在不同位置处的线程。
关于android - 使用 OneDrive SDK 在 AsyncTask 中下载文件抛出 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38716915/
今天我在一个 Java 应用程序中看到了几种不同的加载文件的方法。 文件:/ 文件:// 文件:/// 这三个 URL 开头有什么区别?使用它们的首选方式是什么? 非常感谢 斯特凡 最佳答案 file
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我有一个 javascript 文件,并且在该方法中有一个“测试”方法,我喜欢调用 C# 函数。 c# 函数与 javascript 文件不在同一文件中。 它位于 .cs 文件中。那么我该如何管理 j
需要检查我使用的文件/目录的权限 //filePath = path of file/directory access denied by user ( in windows ) File fil
我在一个目录中有很多 java 文件,我想在我的 Intellij 项目中使用它。但是我不想每次开始一个新项目时都将 java 文件复制到我的项目中。 我知道我可以在 Visual Studio 和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有 3 个组件的 Twig 文件: 文件 1: {# content-here #} 文件 2: {{ title-here }} {# content-here #}
我得到了 mod_ldap.c 和 mod_authnz_ldap.c 文件。我需要使用 Linux 命令的 mod_ldap.so 和 mod_authnz_ldap.so 文件。 最佳答案 从 c
我想使用PIE在我的项目中使用 IE7。 但是我不明白的是,我只能在网络服务器上使用 .htc 文件吗? 我可以在没有网络服务器的情况下通过浏览器加载的本地页面中使用它吗? 我在 PIE 的文档中看到
我在 CI 管道中考虑这一点,我应该首先构建和测试我的应用程序,结果应该是一个 docker 镜像。 我想知道使用构建环境在构建服务器上构建然后运行测试是否更常见。也许为此使用构建脚本。最后只需将 j
using namespace std; struct WebSites { string siteName; int rank; string getSiteName() {
我是 Linux 新手,目前正在尝试使用 ginkgo USB-CAN 接口(interface) 的 API 编程功能。为了使用 C++ 对 API 进行编程,他们提供了库文件,其中包含三个带有 .
我刚学C语言,在实现一个程序时遇到了问题将 test.txt 文件作为程序的输入。 test.txt 文件的内容是: 1 30 30 40 50 60 2 40 30 50 60 60 3 30 20
如何连接两个tcpdump文件,使一个流量在文件中出现一个接一个?具体来说,我想“乘以”一个 tcpdump 文件,这样所有的 session 将一个接一个地按顺序重复几次。 最佳答案 mergeca
我有一个名为 input.MP4 的文件,它已损坏。它来自闭路电视摄像机。我什么都试过了,ffmpeg , VLC 转换,没有运气。但是,我使用了 mediainfo和 exiftool并提取以下信息
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
给定一个 gzip 文件 Z,如果我将其解压缩为 Z',有什么办法可以重新压缩它以恢复完全相同的 gzip 文件 Z?在粗略阅读了 DEFLATE 格式后,我猜不会,因为任何给定的文件都可能在 DEF
我必须从数据库向我的邮件 ID 发送一封带有附件的邮件。 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Adventure Works Admin
我有一个大的 M4B 文件和一个 CUE 文件。我想将其拆分为多个 M4B 文件,或将其拆分为多个 MP3 文件(以前首选)。 我想在命令行中执行此操作(OS X,但如果需要可以使用 Linux),而
快速提问。我有一个没有实现文件的类的项目。 然后在 AppDelegate 我有: #import "AppDelegate.h" #import "SomeClass.h" @interface A
我是一名优秀的程序员,十分优秀!