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
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
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