Android Question Firebase database and JSON

mariosteffen

Member
Licensed User
Longtime User
Does anybody have an example of B4A code to write JSON strings on a Firebase Database using HTTP POST method ?

Thanks.
 

mariosteffen

Member
Licensed User
Longtime User
My code is:
Dim fullname As String
Dim UrlFIrebase As String
Dim a As Map
a.Initialize
a.Put("name","xyz")
a.Put("family","abcd")
Dim Jsongen As JSONGenerator
Jsongen.Initialize(a)
fullname=Jsongen.ToString
Dim b As HttpRequest

UrlFIrebase="https://abcxyz.firebaseio.com/test/2.json"
b.InitializePost2(UrlFIrebase,fullname.GetBytes("UTF8"))
b.SetContentType("application/json")
Dim CliHttp As HttpClient
CliHttp.Initialize("CliHttp")
CliHttp.Execute(b,1)

I am using B4A version 8 and the last version of the B4A bridge. The libraries are Core, HTTP, JSON and OkHttp.
If I test on a real device on debug mode, it works fine, but if I change to Release mode the program writes the data and stops after that with the error "B4A example stopped".

Any ideas to solve this ?

Thanks in advance.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.
 
Upvote 0

mariosteffen

Member
Licensed User
Longtime User
Hi, DonManfred. Thanks for your help.

I added the BridgeLogger: true option and the result is below:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.Exception: Sub clihttp_responsesuccess was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:192)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

mariosteffen

Member
Licensed User
Longtime User
Hi, DonManfred.
I changed to OkHttpUtils2 and the problem still happens.
I used a cell phone with Android 5, another cell phone with Android 7 and a tablet witht Android 4.4.

Logger connected to: motorola XT1572
--------- beginning of crash
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.Exception: Sub clihttp_responsesuccess was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:192)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

Any ideas ? As I said on the first message, my needs are really simple. I want to write json strings on a firebase database. I could not find any example on the forums.

Regards.
 
Upvote 0

mariosteffen

Member
Licensed User
Longtime User
Hi, Manfred. I solved my problem with the code below, based on this post: https://www.b4x.com/android/forum/t...s-firebase-cloud-messaging-fcm.67716/#content

Thanks for your help.

Sub WriteJson(Key As String, Value As String)
Dim Job As HttpJob
Job.Initialize("fcm", Me)
Dim m As Map = CreateMap(Key:Value)
Dim jg As JSONGenerator
jg.Initialize(m)
Job.PostString("https://abcxyz.firebaseio.com/test/abc.json", jg.ToString)
Job.GetRequest.SetContentType("application/json;charset=UTF-8")
Job.GetRequest.SetHeader("Authorization", "key=" & "put the auth key here")
End Sub

Sub JobDone (Job As HttpJob)
Log(Job)
If Job.Success Then
Log(Job.GetString)
End If
Job.Release
End Sub
 
Upvote 0
Top