Android Question Help, not very good with xml or json so any help would be greatly appreciated

Colin Evans

Active Member
Licensed User
Longtime User
Hi, trying to create a weather app with APIXU json data and whilst the site gives very good examples of how to enter the http to retrieve the information I cannot get my head around how to action the http and then use the resulting JSON data it returns

So the call would be

http://api.apixu.com/v1/forecast.json?key=< My Key >&q=rhyl&days=10

and it returns, the data below but how would I use this data to create the ten days of data in returns, any idea's would be greatly appreciated but please remember I am very new to this and would need a lot of hand holding

Response Code
200
Response Headers
{
"access-control-allow-origin": "*",
"access-control-allow-headers": "content-type",
"Vary": "Accept-Encoding",
"Age": "0",
"Connection": "keep-alive",
"Accept-Ranges": "bytes",
"Content-Length": "4145",
"Cache-Control": "private",
"Content-Type": "application/json; charset=utf-8",
"Date": "Tue, 22 May 2018 16:49:11 GMT",
"Via": "WebCelerate"
}
Response Body
{
"location": {
"name": "Rhyl",
"region": "Denbighshire",
"country": "United Kingdom",
"lat": 53.32,
"lon": -3.48,
"tz_id": "Europe/London",
"localtime_epoch": 1527007751,
"localtime": "2018-05-22 17:49"
},
"current": {
"last_updated_epoch": 1527007519,
"last_updated": "2018-05-22 17:45",
"temp_c": 19.0,
"temp_f": 66.2,
"is_day": 1,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"wind_mph": 13.6,
"wind_kph": 22.0,
"wind_degree": 310,
"wind_dir": "NW",
"pressure_mb": 1021.0,
"pressure_in": 30.6,
"precip_mm": 0.0,
"precip_in": 0.0,
"humidity": 60,
"cloud": 25,
"feelslike_c": 19.0,
"feelslike_f": 66.2,
"vis_km": 10.0,
"vis_miles": 6.0
},
"forecast": {
"forecastday": [
{
"date": "2018-05-22",
"date_epoch": 1526947200,
"day": {
"maxtemp_c": 15.2,
"maxtemp_f": 59.4,
"mintemp_c": 10.4,
"mintemp_f": 50.7,
"avgtemp_c": 12.3,
"avgtemp_f": 54.1,
"maxwind_mph": 10.1,
"maxwind_kph": 16.2,
"totalprecip_mm": 0.0,
"totalprecip_in": 0.0,
"avgvis_km": 18.2,
"avgvis_miles": 11.0,
"avghumidity": 86.0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"uv": 6.1
},
"astro": {
"sunrise": "05:04 AM",
"sunset": "09:18 PM",
"moonrise": "12:43 PM",
"moonset": "02:38 AM"
}
},
{
"date": "2018-05-23",
"date_epoch": 1527033600,
"day": {
"maxtemp_c": 18.1,
"maxtemp_f": 64.6,
"mintemp_c": 11.0,
"mintemp_f": 51.8,
"avgtemp_c": 14.3,
"avgtemp_f": 57.7,
"maxwind_mph": 8.1,
"maxwind_kph": 13.0,
"totalprecip_mm": 0.0,
"totalprecip_in": 0.0,
"avgvis_km": 19.8,
"avgvis_miles": 12.0,
"avghumidity": 77.0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"uv": 6.5
},
"astro": {
"sunrise": "05:03 AM",
"sunset": "09:20 PM",
"moonrise": "02:01 PM",
"moonset": "03:05 AM"
}
},
{
"date": "2018-05-24",
"date_epoch": 1527120000,
"day": {
"maxtemp_c": 17.1,
"maxtemp_f": 62.8,
"mintemp_c": 10.1,
"mintemp_f": 50.2,
"avgtemp_c": 14.3,
"avgtemp_f": 57.8,
"maxwind_mph": 11.2,
"maxwind_kph": 18.0,
"totalprecip_mm": 0.0,
"totalprecip_in": 0.0,
"avgvis_km": 17.3,
"avgvis_miles": 10.0,
"avghumidity": 76.0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"uv": 6.0
},
"astro": {
"sunrise": "05:01 AM",
"sunset": "09:21 PM",
"moonrise": "03:16 PM",
"moonset": "03:28 AM"
}
},
{
"date": "2018-05-25",
"date_epoch": 1527206400,
"day": {
"maxtemp_c": 17.5,
"maxtemp_f": 63.5,
"mintemp_c": 10.2,
"mintemp_f": 50.4,
"avgtemp_c": 14.0,
"avgtemp_f": 57.1,
"maxwind_mph": 14.3,
"maxwind_kph": 23.0,
"totalprecip_mm": 0.0,
"totalprecip_in": 0.0,
"avgvis_km": 15.6,
"avgvis_miles": 9.0,
"avghumidity": 75.0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"uv": 5.9
},
"astro": {
"sunrise": "05:00 AM",
"sunset": "09:23 PM",
"moonrise": "04:30 PM",
"moonset": "03:49 AM"
}
},
{
"date": "2018-05-26",
"date_epoch": 1527292800,
"day": {
"maxtemp_c": 17.7,
"maxtemp_f": 63.9,
"mintemp_c": 10.1,
"mintemp_f": 50.2,
"avgtemp_c": 13.9,
"avgtemp_f": 57.0,
"maxwind_mph": 16.1,
"maxwind_kph": 25.9,
"totalprecip_mm": 0.0,
"totalprecip_in": 0.0,
"avgvis_km": 16.7,
"avgvis_miles": 10.0,
"avghumidity": 74.0,
"condition": {
"text": "Partly cloudy",
"icon": "//cdn.apixu.com/weather/64x64/day/116.png",
"code": 1003
},
"uv": 4.5
},
"astro": {
"sunrise": "04:59 AM",
"sunset": "09:24 PM",
"moonrise": "05:43 PM",
"moonset": "04:09 AM"
}
},
{
"date": "2018-05-27",
"date_epoch": 1527379200,
"day": {
"maxtemp_c": 17.3,
"maxtemp_f": 63.1,
"mintemp_c": 12.8,
"mintemp_f": 55.0,
"avgtemp_c": 14.1,
"avgtemp_f": 57.3,
"maxwind_mph": 13.9,
"maxwind_kph": 22.3,
"totalprecip_mm": 0.0,
"totalprecip_in": 0.0,
"avgvis_km": 14.5,
"avgvis_miles": 9.0,
"avghumidity": 81.0,
"condition": {
"text": "Overcast",
"icon": "//cdn.apixu.com/weather/64x64/day/122.png",
"code": 1009
},
"uv": 39960.0
},
"astro": {
"sunrise": "04:58 AM",
"sunset": "09:25 PM",
"moonrise": "06:54 PM",
"moonset": "04:31 AM"
}
},
{
"date": "2018-05-28",
"date_epoch": 1527465600,
"day": {
"maxtemp_c": 16.7,
"maxtemp_f": 62.1,
"mintemp_c": 13.2,
"mintemp_f": 55.8,
"avgtemp_c": 14.8,
"avgtemp_f": 58.7,
"maxwind_mph": 10.3,
"maxwind_kph": 16.6,
"totalprecip_mm": 3.4,
"totalprecip_in": 0.13,
"avgvis_km": 12.1,
"avgvis_miles": 7.0,
"avghumidity": 88.0,
"condition": {
"text": "Moderate or heavy rain shower",
"icon": "//cdn.apixu.com/weather/64x64/day/356.png",
"code": 1243
},
"uv": 39960.0
},
"astro": {
"sunrise": "04:57 AM",
"sunset": "09:27 PM",
"moonrise": "08:03 PM",
"moonset": "04:55 AM"
}
}
]
}
}
 

Douglas Farias

Expert
Licensed User
Longtime User
http://www.b4x.com:51042/json/index.html
you can parse the Json here. its a good start



B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim current As Map = root.Get("current")
Dim feelslike_c As Double = current.Get("feelslike_c")
Dim last_updated As String = current.Get("last_updated")
Dim feelslike_f As Double = current.Get("feelslike_f")
Dim wind_degree As Int = current.Get("wind_degree")
Dim last_updated_epoch As Int = current.Get("last_updated_epoch")
Dim is_day As Int = current.Get("is_day")
Dim precip_in As Double = current.Get("precip_in")
Dim wind_dir As String = current.Get("wind_dir")
Dim temp_c As Double = current.Get("temp_c")
Dim pressure_in As Double = current.Get("pressure_in")
Dim temp_f As Double = current.Get("temp_f")
Dim precip_mm As Double = current.Get("precip_mm")
Dim cloud As Int = current.Get("cloud")
Dim wind_kph As Double = current.Get("wind_kph")
Dim condition As Map = current.Get("condition")
Dim code As Int = condition.Get("code")
Dim icon As String = condition.Get("icon")
Dim text As String = condition.Get("text")
Dim wind_mph As Double = current.Get("wind_mph")
Dim vis_km As Double = current.Get("vis_km")
Dim humidity As Int = current.Get("humidity")
Dim pressure_mb As Double = current.Get("pressure_mb")
Dim vis_miles As Double = current.Get("vis_miles")
Dim location As Map = root.Get("location")
Dim localtime As String = location.Get("localtime")
Dim country As String = location.Get("country")
Dim localtime_epoch As Int = location.Get("localtime_epoch")
Dim name As String = location.Get("name")
Dim lon As Double = location.Get("lon")
Dim region As String = location.Get("region")
Dim lat As Double = location.Get("lat")
Dim tz_id As String = location.Get("tz_id")
Dim forecast As Map = root.Get("forecast")
Dim forecastday As List = forecast.Get("forecastday")
For Each colforecastday As Map In forecastday
 Dim date As String = colforecastday.Get("date")
 Dim astro As Map = colforecastday.Get("astro")
 Dim moonset As String = astro.Get("moonset")
 Dim sunrise As String = astro.Get("sunrise")
 Dim sunset As String = astro.Get("sunset")
 Dim moonrise As String = astro.Get("moonrise")
 Dim date_epoch As Int = colforecastday.Get("date_epoch")
 Dim day As Map = colforecastday.Get("day")
 Dim avgvis_km As Double = day.Get("avgvis_km")
 Dim uv As Double = day.Get("uv")
 Dim avgtemp_f As Double = day.Get("avgtemp_f")
 Dim avgtemp_c As Double = day.Get("avgtemp_c")
 Dim maxtemp_c As Double = day.Get("maxtemp_c")
 Dim maxtemp_f As Double = day.Get("maxtemp_f")
 Dim mintemp_c As Double = day.Get("mintemp_c")
 Dim avgvis_miles As Double = day.Get("avgvis_miles")
 Dim mintemp_f As Double = day.Get("mintemp_f")
 Dim totalprecip_in As Double = day.Get("totalprecip_in")
 Dim avghumidity As Double = day.Get("avghumidity")
 Dim condition As Map = day.Get("condition")
 Dim code As Int = condition.Get("code")
 Dim icon As String = condition.Get("icon")
 Dim text As String = condition.Get("text")
 Dim maxwind_kph As Double = day.Get("maxwind_kph")
 Dim maxwind_mph As Double = day.Get("maxwind_mph")
 Dim totalprecip_mm As Double = day.Get("totalprecip_mm")
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
So the call would be

http://api.apixu.com/v1/forecast.json?key=< My Key >&q=rhyl&days=10
use okhttputils to call the site....

wait for the JobDone raised and use this code with the result

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim current As Map = root.Get("current")
Dim feelslike_c As Double = current.Get("feelslike_c")
Dim last_updated As String = current.Get("last_updated")
Dim feelslike_f As Double = current.Get("feelslike_f")
Dim wind_degree As Int = current.Get("wind_degree")
Dim last_updated_epoch As Int = current.Get("last_updated_epoch")
Dim is_day As Int = current.Get("is_day")
Dim precip_in As Double = current.Get("precip_in")
Dim wind_dir As String = current.Get("wind_dir")
Dim temp_c As Double = current.Get("temp_c")
Dim pressure_in As Double = current.Get("pressure_in")
Dim temp_f As Double = current.Get("temp_f")
Dim precip_mm As Double = current.Get("precip_mm")
Dim cloud As Int = current.Get("cloud")
Dim wind_kph As Double = current.Get("wind_kph")
Dim condition As Map = current.Get("condition")
Dim code As Int = condition.Get("code")
Dim icon As String = condition.Get("icon")
Dim text As String = condition.Get("text")
Dim wind_mph As Double = current.Get("wind_mph")
Dim vis_km As Double = current.Get("vis_km")
Dim humidity As Int = current.Get("humidity")
Dim pressure_mb As Double = current.Get("pressure_mb")
Dim vis_miles As Double = current.Get("vis_miles")
Dim location As Map = root.Get("location")
Dim localtime As String = location.Get("localtime")
Dim country As String = location.Get("country")
Dim localtime_epoch As Int = location.Get("localtime_epoch")
Dim name As String = location.Get("name")
Dim lon As Double = location.Get("lon")
Dim region As String = location.Get("region")
Dim lat As Double = location.Get("lat")
Dim tz_id As String = location.Get("tz_id")
Dim forecast As Map = root.Get("forecast")
Dim forecastday As List = forecast.Get("forecastday")
For Each colforecastday As Map In forecastday
 Dim date As String = colforecastday.Get("date")
 Dim astro As Map = colforecastday.Get("astro")
 Dim moonset As String = astro.Get("moonset")
 Dim sunrise As String = astro.Get("sunrise")
 Dim sunset As String = astro.Get("sunset")
 Dim moonrise As String = astro.Get("moonrise")
 Dim date_epoch As Int = colforecastday.Get("date_epoch")
 Dim day As Map = colforecastday.Get("day")
 Dim avgvis_km As Double = day.Get("avgvis_km")
 Dim uv As Double = day.Get("uv")
 Dim avgtemp_f As Double = day.Get("avgtemp_f")
 Dim avgtemp_c As Double = day.Get("avgtemp_c")
 Dim maxtemp_c As Double = day.Get("maxtemp_c")
 Dim maxtemp_f As Double = day.Get("maxtemp_f")
 Dim mintemp_c As Double = day.Get("mintemp_c")
 Dim avgvis_miles As Double = day.Get("avgvis_miles")
 Dim mintemp_f As Double = day.Get("mintemp_f")
 Dim totalprecip_in As Double = day.Get("totalprecip_in")
 Dim avghumidity As Double = day.Get("avghumidity")
 Dim condition As Map = day.Get("condition")
 Dim code As Int = condition.Get("code")
 Dim icon As String = condition.Get("icon")
 Dim text As String = condition.Get("text")
 Dim maxwind_kph As Double = day.Get("maxwind_kph")
 Dim maxwind_mph As Double = day.Get("maxwind_mph")
 Dim totalprecip_mm As Double = day.Get("totalprecip_mm")
Next

Reference: http://basic4ppc.com:51042/json/index.html
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Many thanks for taking the time to reply, I'll have a play and see if I can turn it into a program, just one quick question given the 'for next' loop would i have to assign each day's value's to the next page displayed etc.
Thanks again really appreciate it
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Where does the variable text come from

parser.Initialize(<text>)

as I get an error

Undeclared variable ' text' is used before it was assigned any value
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
Where does the variable text come from

parser.Initialize(<text>)

as I get an error

Undeclared variable ' text' is used before it was assigned any value

put in your json string, that thing with "{...}"
its the result Content-Type application/json with charset utf-8 from your request
that you saved into a string variable.

the webrequest you make with libs okhttputils2 & okhttp,
just search for okhttputils2 and you will find examples.

see DownloadQuote example at this page
https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/#content
 
Last edited:
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Still not getting anywhere the following returns 5 java errors, really appreciate the help so far hopefully I'm not too far away, hope you can help

Sub Download (Callback As Object, link As String) As HttpJob
Dim j As HttpJob
j.Initialize("", Callback)
j.Download(link)
Return j
End Sub

Sub Activity_Create(FirstTime As Boolean)
Wait For (Download(Me, "http://api.apixu.com/v1/forecast.json?key=4ac99b55f7944c1e91e121155181105&q=rhyl&days=7")) JobDone (j As HttpJob)
If j.Success Then
Log(j.GetString)
Activity.LoadLayout("Layout1")
Dim parser As JSONParser
parser.Initialize(j)
Dim root As Map = parser.NextObject
Dim current As Map = root.Get("current")
Dim feelslike_c As Double = current.Get("feelslike_c")
Dim last_updated As String = current.Get("last_updated")
Dim feelslike_f As Double = current.Get("feelslike_f")
Dim wind_degree As Int = current.Get("wind_degree")
Dim last_updated_epoch As Int = current.Get("last_updated_epoch")
Dim is_day As Int = current.Get("is_day")
Dim precip_in As Double = current.Get("precip_in")
Dim wind_dir As String = current.Get("wind_dir")
Dim temp_c As Double = current.Get("temp_c")
Dim pressure_in As Double = current.Get("pressure_in")
Dim temp_f As Double = current.Get("temp_f")
Dim precip_mm As Double = current.Get("precip_mm")
Dim cloud As Int = current.Get("cloud")
Dim wind_kph As Double = current.Get("wind_kph")
Dim condition As Map = current.Get("condition")
Dim code As Int = condition.Get("code")
Dim icon As String = condition.Get("icon")
Dim text As String = condition.Get("text")
Dim wind_mph As Double = current.Get("wind_mph")
Dim vis_km As Double = current.Get("vis_km")
Dim humidity As Int = current.Get("humidity")
Dim pressure_mb As Double = current.Get("pressure_mb")
Dim vis_miles As Double = current.Get("vis_miles")
Dim location As Map = root.Get("location")
Dim localtime As String = location.Get("localtime")
Dim country As String = location.Get("country")
Dim localtime_epoch As Int = location.Get("localtime_epoch")
Dim name As String = location.Get("name")
Dim lon As Double = location.Get("lon")
Dim region As String = location.Get("region")
Dim lat As Double = location.Get("lat")
Dim tz_id As String = location.Get("tz_id")
Dim forecast As Map = root.Get("forecast")
Dim forecastday As List = forecast.Get("forecastday")
For Each colforecastday As Map In forecastday
Dim date As String = colforecastday.Get("date")
Dim astro As Map = colforecastday.Get("astro")
Dim moonset As String = astro.Get("moonset")
Dim sunrise As String = astro.Get("sunrise")
Dim sunset As String = astro.Get("sunset")
Dim moonrise As String = astro.Get("moonrise")
Dim date_epoch As Int = colforecastday.Get("date_epoch")
Dim day As Map = colforecastday.Get("day")
Dim avgvis_km As Double = day.Get("avgvis_km")
Dim uv As Double = day.Get("uv")
Dim avgtemp_f As Double = day.Get("avgtemp_f")
Dim avgtemp_c As Double = day.Get("avgtemp_c")
Dim maxtemp_c As Double = day.Get("maxtemp_c")
Dim maxtemp_f As Double = day.Get("maxtemp_f")
Dim mintemp_c As Double = day.Get("mintemp_c")
Dim avgvis_miles As Double = day.Get("avgvis_miles")
Dim mintemp_f As Double = day.Get("mintemp_f")
Dim totalprecip_in As Double = day.Get("totalprecip_in")
Dim avghumidity As Double = day.Get("avghumidity")
Dim condition As Map = day.Get("condition")
Dim code As Int = condition.Get("code")
Dim icon As String = condition.Get("icon")
Dim text As String = condition.Get("text")
Dim maxwind_kph As Double = day.Get("maxwind_kph")
Dim maxwind_mph As Double = day.Get("maxwind_mph")
Dim totalprecip_mm As Double = day.Get("totalprecip_mm")
Next
End If
j.Release
End Sub

errors when trying to run

B4A version: 7.00
Parsing code. (0.00s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Generating R file. (0.03s)
Compiling generated Java code. (0.73s)
Convert byte code - optimized dex. Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httpjob;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service$2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/samples/httputils2/httputils2service$httputils2service_BR;
5 errors; aborting
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Brilliant, works okay, I'll crack on and try to learn as I go, thank you again for taking the time to help, really appreciated
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
that is wrong you should give the result string here not the HttpJob object
Log(j.GetString)
Dim parser As JSONParser
parser.Initialize(j.GetString)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi 1lusca many thanks for all your help but if I may ask one more thing, how do I then put the values into my fields on screen, I basically created a few labels and then tried to assign the variables defined in the code, i.e. Label1.text = name Label2.text = country Label3.text = region etc but it states the variables aren't defined but even if I defined them in the globals sub I don't get anything on screen.

I'm obviously not assigning them correctly or not in the correct area of the program

Sorry to keep asking but I am trying

I assume MarkusR (thanks for replying) is referring to my code given yours seems to work
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
I assume MarkusR (thanks for replying) is referring to my code given yours seems to work
yes, i spoke about your post above here.
B4X:
Sub Activity_Create(FirstTime As Boolean)
Wait For (Download(Me, "http://api.apixu.com/v1/forecast.json?key=4ac99b55f7944c1e91e121155181105&q=rhyl&days=7")) JobDone (j As HttpJob)
If j.Success Then
Log(j.GetString)
Activity.LoadLayout("Layout1")
Dim parser As JSONParser
parser.Initialize(j)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Think I've solved it, basically assign the labels etc before then assign them in the parsing code instead of Dim name as String = location.get("name") I just entered Label1.text = location.get("name")

Just one question in the for Next loop what can I use to assign to my labels for each day, i.e. if there are seven days of data and I want to display each day on a different screen, normally I'd use a for next loop that assigns a value i.e. For i = 1 to 7 ... day(1)=... day(2)=... etx

Hope that makes sense
Cheers
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Figured it out, many thanks for all your help

Dim forecast As Map = root.Get("forecast")
'
Dim forecastday As List = forecast.Get("forecastday")
'
Dim col0 As Map = forecastday.Get("0")
Dim date0 As String = col0.Get("date")
lblDate0.Text = date0
Dim col1 As Map = forecastday.Get("1")
Dim date1 As String = col1.Get("date")
lblDate1.Text = date1
Dim col2 As Map = forecastday.Get("2")
Dim date2 As String = col2.Get("date")
lblDate2.Text = date2
 
Upvote 0
Top