我正试图在Alfresco的文件夹中上传一个文件,但我遇到了这个异常,我在过去两天试图解决这个问题,但没有任何机会。
我使用的是安卓版Alfresco Mobile SDK和AlfrescoCommunity 4.2。
以下是我如何上传文件,如果我做错了什么,请告诉我:
File fileToUpload = new File(selectedFilePath);
if((fileToUpload != null) && fileToUpload.isFile()) {
Log.d(MainActivity.DEBUGING, "File to upload's name: " + fileToUpload.getName());
String fileName = fileToUpload.getName();
String mimeType = getMimeType(selectedFilePath);
ContentFile contentFile = new ContentFileImpl(fileToUpload, fileName, mimeType);
try {
Log.w(MainActivity.DEBUGING, "create file: " + fileName + " of type: " + mimeType + " in: " + selectedFilePath);
session.getServiceRegistry().getDocumentFolderService().createDocument(currentFolder, fileName, properties, contentFile);
Toast.makeText(this, "file: " + fileName + " of type: " + mimeType + "in: " + selectedFilePath + " was created", Toast.LENGTH_LONG).show();
}
catch(org.alfresco.mobile.android.api.exceptions.AlfrescoServiceException e) {
Toast.makeText(this, "Upload failed ! ", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
//
这是我在日志中得到的例外:
04-22 11:29:09.475: W/System.err(616): org.alfresco.mobile.android.api.exceptions.AlfrescoServiceException: org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException: Cannot access http://192.168.1.15:8080/alfresco/api/-default-/public/cmis/versions/1.0/atom/children?id=37f6caba-bd45-4191-96b3-3604d4cbb9be&versioningState=major: null
04-22 11:29:09.475: W/System.err(616): at org.alfresco.mobile.android.api.exceptions.impl.ExceptionHelper.convertException(ExceptionHelper.java:119)
04-22 11:29:09.475: W/System.err(616): at org.alfresco.mobile.android.api.services.impl.AlfrescoService.convertException(AlfrescoService.java:302)
04-22 11:29:09.475: W/System.err(616): at org.alfresco.mobile.android.api.services.impl.AbstractDocumentFolderServiceImpl.createDocument(AbstractDocumentFolderServiceImpl.java:542)
04-22 11:29:09.475: W/System.err(616): at org.alfresco.mobile.android.api.services.impl.AbstractDocumentFolderServiceImpl.createDocument(AbstractDocumentFolderServiceImpl.java:458)
04-22 11:29:09.484: W/System.err(616): at org.valueit.turboecm.MyTurboECMBrowserActivity.onActivityResult(MyTurboECMBrowserActivity.java:364)
04-22 11:29:09.484: W/System.err(616): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
04-22 11:29:09.484: W/System.err(616): at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
04-22 11:29:09.484: W/System.err(616): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
04-22 11:29:09.484: W/System.err(616): at android.app.ActivityThread.access$1100(ActivityThread.java:130)
04-22 11:29:09.494: W/System.err(616): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
04-22 11:29:09.494: W/System.err(616): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:29:09.494: W/System.err(616): at android.os.Looper.loop(Looper.java:137)
04-22 11:29:09.494: W/System.err(616): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-22 11:29:09.504: W/System.err(616): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:29:09.504: W/System.err(616): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:29:09.504: W/System.err(616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-22 11:29:09.504: W/System.err(616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-22 11:29:09.514: W/System.err(616): at dalvik.system.NativeStart.main(Native Method)
04-22 11:29:09.514: W/System.err(616): Caused by: org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException: Cannot access http://192.168.1.15:8080/alfresco/api/-default-/public/cmis/versions/1.0/atom/children?id=37f6caba-bd45-4191-96b3-3604d4cbb9be&versioningState=major: null
04-22 11:29:09.514: W/System.err(616): at org.alfresco.mobile.android.api.network.NetworkHttpInvoker.invoke(NetworkHttpInvoker.java:291)
04-22 11:29:09.514: W/System.err(616): at org.alfresco.mobile.android.api.network.NetworkHttpInvoker.invokePOST(NetworkHttpInvoker.java:74)
04-22 11:29:09.524: W/System.err(616): at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService.post(AbstractAtomPubService.java:616)
04-22 11:29:09.524: W/System.err(616): at org.apache.chemistry.opencmis.client.bindings.spi.atompub.ObjectServiceImpl.createDocument(ObjectServiceImpl.java:116)
04-22 11:29:09.524: W/System.err(616): at org.alfresco.mobile.android.api.services.impl.AbstractDocumentFolderServiceImpl.createDocument(AbstractDocumentFolderServiceImpl.java:511)
04-22 11:29:09.524: W/System.err(616): ... 15 more
04-22 11:29:09.534: W/System.err(616): Caused by: android.os.NetworkOnMainThreadException
04-22 11:29:09.544: W/System.err(616): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-22 11:29:09.544: W/System.err(616): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-22 11:29:09.544: W/System.err(616): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
04-22 11:29:09.544: W/System.err(616): at libcore.io.IoBridge.connect(IoBridge.java:112)
04-22 11:29:09.554: W/System.err(616): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-22 11:29:09.554: W/System.err(616): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-22 11:29:09.554: W/System.err(616): at java.net.Socket.connect(Socket.java:842)
04-22 11:29:09.564: W/System.err(616): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
04-22 11:29:09.564: W/System.err(616): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-22 11:29:09.564: W/System.err(616): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
04-22 11:29:09.564: W/System.err(616): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-22 11:29:09.564: W/System.err(616): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-22 11:29:09.574: W/System.err(616): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
04-22 11:29:09.574: W/System.err(616): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
04-22 11:29:09.574: W/System.err(616): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
04-22 11:29:09.584: W/System.err(616): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
04-22 11:29:09.584: W/System.err(616): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-22 11:29:09.584: W/System.err(616): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
04-22 11:29:09.584: W/System.err(616): at org.alfresco.mobile.android.api.network.NetworkHttpInvoker.invoke(NetworkHttpInvoker.java:264)
04-22 11:29:09.584: W/System.err(616): ... 19 more
04-22 11:29:09.596: W/debogage(616): ====================> filePath: /dev/10173306_804433689584264_1962438159_n.jpg
//我也尝试过在后台创建文件,但这一次,当loadInBackGround启动时,我的应用程序出现黑屏,过了一段时间,应用程序停止响应:(
DocumentCreateLoader contentCreator = new DocumentCreateLoader(this, session, currentFolder, fileName, properties, contentFile);
contentCreator.loadInBackground();
Aloah,
只是其他开发人员的一个快速提示:
如果你在崩溃后在日志中看到这一行
04-22 11:29:09.334:W/System.err(616):由以下原因引起:android.os.NetworkOnMainThreadException
这意味着您需要在后台线程中运行代码,如AsyncTask、Loader、Thread。。。而不是应用程序的Main/UI线程。任何与网络相关的操作都必须在Android的主线程之外进行。