Question

I am trying to upload a file inside a folder on Alfresco, but i am getting this exception that i tried to solve this past two days without any chance.

I am using the Alfresco Mobile SDK for Android, and alfresco Community 4.2.

here is how i try to upload a file, tell me if i am doing something wrong:

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();
                }
            }

//

this is the exception that am getting in the Log:

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

//I have also tried this to create the file in background but this time i get a black screen in my app when loadInBackgroud starts, and after a while the app stops responding :(

DocumentCreateLoader contentCreator = new DocumentCreateLoader(this, session, currentFolder, fileName, properties, contentFile);
contentCreator.loadInBackground();
Was it helpful?

Solution

Aloah,

Just a quick tip for others dev :

If you see this line in your log after a crash

04-22 11:29:09.534: W/System.err(616): Caused by: android.os.NetworkOnMainThreadException

It means you need to run the code inside a Background Thread like AsyncTask, Loader, Thread... instead of the Main/UI Thread of your Application. Any network related operation must be done outside the main thread in Android.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top