Android Question Download JSON file and turn it into strings.

TheSketchees

Member
Licensed User
Longtime User
Hello Guys,
I'm wanting to download this JSON file (https://thingspeak.com/channels/144263/feed/last.json) and turn it into strings for my app. I've managed to write the part for the parser and when manually downloaded it works. But I cannot seem to get my head around HttpUtils2 to download this as a string.

This is the part i got so far.
B4X:
Dim parser As JSONParser
    parser.Initialize(The JSON FILE HERE)
    Dim root As Map = parser.NextObject
    Dim field1 As String = root.Get("field1")
    Dim created_at As String = root.Get("created_at")
    Dim field3 As String = root.Get("field3")
    Dim entry_id As Int = root.Get("entry_id")
    Dim field2 As String = root.Get("field2")
    Dim field4 As String = root.Get("field4")
    Log(field1)

Thanks,
Alex
 

TheSketchees

Member
Licensed User
Longtime User
I now have it working actually.
B4X:
Sub CheckTempSub
    Dim CheckTemp As HttpJob
    CheckTemp.Initialize("CheckTemp", Me)
    CheckTemp.Download("https://thingspeak.com/channels/144263/feed/last.json")
    CheckTemp.Release
End Sub
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job"
                'print the result to the logs
                Log("OpenBot" & Job.GetString)
            Case "CheckTemp"
                Log("CheckTemp" & Job.GetString)
                Dim parser As JSONParser
                parser.Initialize(Job.GetString)
                Dim root As Map = parser.NextObject
                Dim field1 As String = root.Get("field1")
                Dim created_at As String = root.Get("created_at")
                Dim field3 As String = root.Get("field3")
                Dim entry_id As Int = root.Get("entry_id")
                Dim field2 As String = root.Get("field2")
                Dim field4 As String = root.Get("field4")
                Log(field1)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

But when i click it a second time i get the error
B4X:
java.lang.RuntimeException: java.io.FileNotFoundException: /data/user/0/HectoSpark.LHSOPENBOT/cache/4: open failed: ENOENT (No such file or directory)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:206)
    at anywheresoftware.b4a.BA$2.run(BA.java:328)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.io.FileNotFoundException: /data/user/0/HectoSpark.LHSOPENBOT/cache/4: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:452)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
    at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:370)
    at anywheresoftware.b4a.samples.httputils2.httputils2service._hc_responsesuccess(httputils2service.java:130)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    ... 8 more
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    at libcore.io.IoBridge.open(IoBridge.java:438)
    ... 13 more
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0
Top