Android Question HTTPUtils2 and JSON error

TrisectDevelopment

Active Member
Licensed User
Longtime User
I'm having a little trouble with httputils2 and json.

I have two URLs and one of them Work in my program but the other does not.
Funny part is if I enter the two URLs in my browser they both return an json file.

But in my code I get an error on one of them.

URL1: (this one is giving me an error in B4A)
http://www.broens-byfest.dk/?json=get_category_posts&category_slug=Nyheder

URL2: (this one works okay. I know its not the same result)
http://www.broens-byfest.dk/?json=get_tag_post&tag_slug=nyheder

This is my download code:
B4X:
        nyhedsJob.Initialize("nyhedsJob", Me)
        nyhedsJob.Download2(UrlNyheder, Array As String("first key", "first value", "second key", "second value"))

This is my JobDone code
B4X:
Sub JobDone(Job As HttpJob)
    Log("string = " & Job.GetString)
    If Job.Success = True Then
        Select Job.JobName
            Case "nyhedsJob"
                '** Det hentede JSON skal gemmes på telefonen
                Dim s As String
                Dim TextWriteNyheder As TextWriter
              
                s = Job.GetString
                TextWriteNyheder.Initialize(File.OpenOutput(File.DirInternal, "nyheder.json", False))
                TextWriteNyheder.Write(s)
                TextWriteNyheder.Close
              
                '** Parse det hentede JSON data
                ParseJson
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
End Sub
The ParseJson is the sub to parse the json, but in URL1 the s variable gives me a string with error.
B4X:
{"status":"error","error":"Not found."}
I cant figure out what is wrong. Can any one help?
 

DonManfred

Expert
Licensed User
Longtime User
try
B4X:
nyhedsJob.Initialize("nyhedsJob", Me)
nyhedsJob.Download2("http://www.broens-byfest.dk/", Array As String("json", "get_category_posts", "category_slug", "category_slug=Nyheder"))

btw:
You should give ParseJson a parameter...

B4X:
ParseJson(s)

B4X:
sub Parsejson(json as string)
Dim parser As JSONParser
parser.Initialize(json)
Dim root As Map = parser.NextObject
Dim category As Map = root.Get("category")
Dim id As Int = category.Get("id")
Dim title As String = category.Get("title")
Dim description As String = category.Get("description")
Dim post_count As Int = category.Get("post_count")
Dim parent As Int = category.Get("parent")
Dim slug As String = category.Get("slug")
Dim count As Int = root.Get("count")
Dim status As String = root.Get("status")
Dim pages As Int = root.Get("pages")
Dim posts As List = root.Get("posts")
For Each colposts As Map In posts
Dim custom_fields As Map = colposts.Get("custom_fields")
Dim comment_count As Int = colposts.Get("comment_count")
Dim tags As List = colposts.Get("tags")
Dim status As String = colposts.Get("status")
Dim excerpt As String = colposts.Get("excerpt")
Dim comment_status As String = colposts.Get("comment_status")
Dim type As String = colposts.Get("type")
Dim date As String = colposts.Get("date")
Dim url As String = colposts.Get("url")
Dim modified As String = colposts.Get("modified")
Dim id As Int = colposts.Get("id")
Dim content As String = colposts.Get("content")
Dim author As Map = colposts.Get("author")
Dim id As Int = author.Get("id")
Dim first_name As String = author.Get("first_name")
Dim nickname As String = author.Get("nickname")
Dim description As String = author.Get("description")
Dim name As String = author.Get("name")
Dim last_name As String = author.Get("last_name")
Dim slug As String = author.Get("slug")
Dim url As String = author.Get("url")
Dim title As String = colposts.Get("title")
Dim slug As String = colposts.Get("slug")
Dim categories As List = colposts.Get("categories")
For Each colcategories As Map In categories
  Dim id As Int = colcategories.Get("id")
  Dim title As String = colcategories.Get("title")
  Dim description As String = colcategories.Get("description")
  Dim post_count As Int = colcategories.Get("post_count")
  Dim parent As Int = colcategories.Get("parent")
  Dim slug As String = colcategories.Get("slug")
Next
Dim attachments As List = colposts.Get("attachments")
Dim comments As List = colposts.Get("comments")
Dim title_plain As String = colposts.Get("title_plain")
Next
end sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i think i found the error... In #3 i suggested the code
B4X:
nyhedsJob.Initialize("nyhedsJob", Me)
nyhedsJob.Download2("http://www.broens-byfest.dk/", Array As String("json", "get_category_posts", "category_slug", "category_slug=Nyheder"))
but the last parameter should be
"Nyheder"
instead of
"category_slug=Nyheder"
. I did not test it now...

Sorry, my mistake :rolleyes:
 
Upvote 0
Top