Android Question SMB File Transfer to Server: NetworkOnMainThreadException

DirkH

Member
Hello,

My app transfers files from the device to the server. I was not able to overcome the error: (NetworkOnMainThreadException) android.os.NetworkOnMainThreadException

I searched the forum and found many, mainly older discussions. Advices used tricks like theDisableStrictMode or didn't fit for my use case.

I tried JavaRunnable with inline Java, that gave the error: (NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.util.Properties.getProperty(java.lang.String)' on a null object reference

B4X:
Private Sub btnCopy_Click
    Log("➡️ Button geklickt – starte SMB-Kontext.")
    
    Dim jo As JavaObject = Me
    Dim r As Object = jo.CreateEvent("java.lang.Runnable", "SMBKontextRunnable", Null)
    
    Dim t As JavaObject
    t.InitializeNewInstance("java.lang.Thread", Array(r))
    t.RunMethod("start", Null)
End Sub

Private Sub SMBKontextRunnable_Event(SMBKontextRunnable As String, Args() As Object) As Object
    Try
        Log("🔧 SMB-Kontextaufbau beginnt...")
        
        Dim baseContext As JavaObject
        baseContext.InitializeStatic("jcifs.context.BaseContext")
        
        Dim config As JavaObject
        config.InitializeNewInstance("jcifs.config.PropertyConfiguration", Array(Null))
        
        Dim benutzer As String = "______"
        Dim passwort As String = "******"
        Dim credentials As JavaObject
        credentials.InitializeNewInstance("jcifs.smb.NtlmPasswordAuthenticator", Array(benutzer, passwort))
        
        Dim instance As JavaObject = baseContext.RunMethod("getInstance", Null)
        smbContext = instance.RunMethod("withCredentials", Array(credentials))
        
        Log("✅ SMB-Kontext aufgebaut.")
    Catch
        Log("❌ Fehler beim SMB-Kontextaufbau: " & LastException)
    End Try
    Return Null
End Sub

#If Java
import anywheresoftware.b4a.BA;

public class JavaRunnable implements java.lang.Runnable {
    BA ba;
    String eventName;

    public JavaRunnable(BA ba, String eventName) {
        this.ba = ba;
        this.eventName = eventName.toLowerCase(BA.cul);
    }

    @Override
    public void run() {
        ba.raiseEvent(null, eventName + "_event");
    }
}
#End If

I tried to use JavaObject and errors like: java.lang.RuntimeException: Method: getInstance not found in: jcifs.context.BaseContext occured and again (NetworkOnMainThreadException) android.os.NetworkOnMainThreadException

What would be the best way to transfer data from the smartphone to a server through WLAN? Are there newer examples that comply with newer rules from google?

Kind regards
Dirk
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The NetworkOnMainThreadException is solved with the DisableStrictMode code. Nothing has changed in this regards since Android 4.

You probably encountered other issues.

What would be the best way to transfer data from the smartphone to a server through WLAN?
Many options. The code you posted is not one of them.
Some examples:
Sockets: https://www.b4x.com/android/forum/t...-asyncstreams-b4xserializator.119011/#content
MQTT: https://www.b4x.com/android/forum/threads/b4x-jmqtt-official-mqtt-client.59472/#content
Http server: search for jServer
FTP: Net library and if you want to implement a server: https://www.b4x.com/android/forum/t...d-with-socket-and-asyncstreams.74320/#content
 
Upvote 0
Top