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.
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?
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
I'm afraid that it is not correct. Both Download and Download2 send a GET request. Download2 url encodes the parameters.
If there are any parameters then you should use Download2. However the link (first parameter) should not include any parameter and should not include the question mark.