sonic18
Lurker
Hello,
This is my first time using json in android, the json is getting downloaded,
i'm just getting an parse error
I understand the concept of json's arrays, objects, keys & values
If you wish to learn more, I would recommend
https://www.tutorialspoint.com/android/android_json_parser.htm
I added a JSON example below
This is my parsing code :
*JSON example:*
Would love to know what my mistake, been looking at it for a while now
This is my first time using json in android, the json is getting downloaded,
i'm just getting an parse error
I understand the concept of json's arrays, objects, keys & values
If you wish to learn more, I would recommend
https://www.tutorialspoint.com/android/android_json_parser.htm
I added a JSON example below
This is my parsing code :
Code:
protected JSONObject doInBackground(Pair<String, String>[] pairs) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/83342?apikey=HDJ7qKj9ZH0XEW41vgj507CVox7GrRE8&language=en-us&details=true&metric=true";
String jsonStr = sh.makeServiceCall(url);
System.out.println("Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray getAccuweather = jsonObj.getJSONArray("weather");
// looping through All Contacts
for (int i = 0; i < getAccuweather.length(); i++) {
JSONObject getInfo = getAccuweather.getJSONObject(i);
String DateTimeArray = getInfo.getString("DateTime");
String EpochDateTimeArray = getInfo.getString("EpochDateTime");
String WeatherIconArray = getInfo.getString("WeatherIcon");
String IconPhraseArray = getInfo.getString("IconPhrase");
String IsDaylighteArray = getInfo.getString("IsDaylight");
String TemperatureeArray = getInfo.getString("Temperature");
String RealFeelTemperatureArray = getInfo.getString("RealFeelTemperature");
String WetBulbTemperatureArray = getInfo.getString("WetBulbTemperature");
String DewPointArray = getInfo.getString("DewPoint");
String WindArray = getInfo.getString("Wind");
String DirectionArray = getInfo.getString("Direction");
String WindGustArray = getInfo.getString("WindGust");
String RelativeHumidityArray = getInfo.getString("RelativeHumidity");
String CeilingArray = getInfo.getString("Ceiling");
String UVIndexArray = getInfo.getString("UVIndex");
String UVIndexTextArray = getInfo.getString("UVIndexText");
String PrecipitationProbabilityArray = getInfo.getString("PrecipitationProbability");
String RainProbabilityArray = getInfo.getString("RainProbability");
String SnowProbabilityArray = getInfo.getString("SnowProbability");
String IceProbabilityArray = getInfo.getString("IceProbability");
String TotalLiquidArray = getInfo.getString("TotalLiquid");
String RainArray = getInfo.getString("Rain");
String SnowArray = getInfo.getString("Snow");
String IceArray = getInfo.getString("Ice");
String CloudCoverArray = getInfo.getString("CloudCover");
String MobileLinkArray = getInfo.getString("MobileLink");
String Link = getInfo.getString("Link");
// JSON Object (Date & Time Object)
JSONObject DateTimeObject = getInfo.getJSONObject("DateTime");
String DateTime = DateTimeObject.getString("DateTime");
// JSON Object (EpochDateTimeO bject)
JSONObject EpochDateObject = getInfo.getJSONObject("EpochDateTime");
int EpochDate = EpochDateObject.getInt("EpochDateTime");
// JSON Object (WeatherIcon Object)
JSONObject WeatherIconObject = getInfo.getJSONObject("WeatherIcon");
int WeatherIcon = WeatherIconObject.getInt("WeatherIcon");
// JSON Object (IconPhrase Object)
JSONObject IconPhraseObject = getInfo.getJSONObject("IconPhrase");
String IconPhrase = IconPhraseObject.getString("IconPhrase");
// JSON Object (IsDaylight Object)
JSONObject IsDaylightObject = getInfo.getJSONObject("IsDaylight");
boolean IsDaylight = IsDaylightObject.getBoolean("IsDaylight");
// JSON Object (Temperature Object)
JSONObject DateTimeObject_Temperature = DateTimeObject.getJSONObject("Temperature");
int DateTimeValue = DateTimeObject_Temperature.getInt("Value");
String Unit = DateTimeObject_Temperature.getString("Unit");
int UnitType = DateTimeObject_Temperature.getInt("UnitType");
// JSON Object (RealFeelTemperature Object)
JSONObject RealFeelTemperatureObject = getInfo.getJSONObject("RealFeelTemperature");
int RealFeelTemperatureValue = RealFeelTemperatureObject.getInt("Value");
String RealFeelTemperatureUnit = RealFeelTemperatureObject.getString("Unit");
int RealFeelTemperatureUnitType = RealFeelTemperatureObject.getInt("UnitType");
// JSON Object (WetBulbTemperature Object)
JSONObject WetBulbTemperatureObject = getInfo.getJSONObject("WetBulbTemperature");
int WetBulbTemperatureValue = WetBulbTemperatureObject.getInt("Value");
String WetBulbTemperatureeUnit = WetBulbTemperatureObject.getString("Unit");
int WetBulbTemperatureType = WetBulbTemperatureObject.getInt("UnitType");
// JSON Object (DewPoint Object)
JSONObject DewPointObject = getInfo.getJSONObject("DewPoint");
int DewPointValue = DewPointObject.getInt("Value");
String DewPointUnit = DewPointObject.getString("Unit");
int DewPointUnitType = DewPointObject.getInt("UnitType");
// JSON Object (Wind Object)
JSONObject WindObject = getInfo.getJSONObject("Wind");
JSONObject WindObject_Wind = WindObject.getJSONObject("Speed");
int WindValue = WindObject_Wind.getInt("Value");
String WindUnit = WindObject_Wind.getString("Unit");
int WindUnitType = WindObject_Wind.getInt("UnitType");
JSONObject WindObject_Direction = getInfo.getJSONObject("Direction");
int Wind_Direction_Degrees = WindObject_Direction.getInt("Degrees");
String Wind_Direction_Localized = WindObject_Direction.getString("Localized");
String Wind_Direction_English = WindObject_Direction.getString("English");
// JSON Object (WindGust Object)
JSONObject WindGustObject = getInfo.getJSONObject("WindGust");
JSONObject WindGustObject_WindGuest = WindGustObject.getJSONObject("Speed");
int WindGuestValue = WindGustObject_WindGuest.getInt("Value");
String WindGuestUnit = WindGustObject_WindGuest.getString("Unit");
int WindGuestUnitType = WindGustObject_WindGuest.getInt("UnitType");
// JSON Object (RelativeHumidity Object)
JSONObject RelativeHumidityObject = getInfo.getJSONObject("RelativeHumidity");
int RelativeHumidityUnit = RelativeHumidityObject.getInt("RelativeHumidity");
JSONObject VisibilityObject = RelativeHumidityObject.getJSONObject("Visibility");
int Visibiliyt_value = VisibilityObject.getInt("Value");
String Visibility_Unit = VisibilityObject.getString("Unit");
int Visibility_UnitType = VisibilityObject.getInt("UnitType");
// JSON Object (Ceiling Object)
JSONObject CeilingObject = getInfo.getJSONObject("Visibility");
int Ceiling_value = CeilingObject.getInt("Value");
String Ceiling_Unit = CeilingObject.getString("Unit");
int Ceiling_UnitType = CeilingObject.getInt("UnitType");
// JSON Object (UVIndex Object)
JSONObject UVIndexObject = getInfo.getJSONObject("UVIndex");
String UVIndex = UVIndexObject.getString("UVIndex");
// JSON Object (PrecipitationProbability Object)
JSONObject PrecipitationProbabilityObject = getInfo.getJSONObject("PrecipitationProbability");
int PrecipitationProbability = PrecipitationProbabilityObject.getInt("PrecipitationProbability");
// JSON Object (UVIndexText Object)
JSONObject UVIndexTextObject = getInfo.getJSONObject("UVIndexText");
int UVIndexText = UVIndexTextObject.getInt("UVIndexText");
// JSON Object (SnowProbability Object)
JSONObject SnowProbabilityObject = getInfo.getJSONObject("SnowProbability");
int SnowProbability = SnowProbabilityObject.getInt("SnowProbability");
// JSON Object (IceProbability Object)
JSONObject IceProbabilityObject = getInfo.getJSONObject("IceProbability");
int IceProbability = IceProbabilityObject.getInt("IceProbability");
// JSON Object (TotalLiquid Object)
JSONObject TotalLiquidObject = getInfo.getJSONObject("TotalLiquid");
int TotalLiquidValue = TotalLiquidObject.getInt("Value");
String TotalLiquidUnit = TotalLiquidObject.getString("Unit");
int TotalLiquidUnitType = TotalLiquidObject.getInt("UnitType");
// JSON Object (Rain Object)
JSONObject RainObject = getInfo.getJSONObject("Rain");
int RainValue = RainObject.getInt("Value");
String RainUnit = RainObject.getString("Unit");
int RainUnitType = RainObject.getInt("UnitType");
// JSON Object (Snow Object)
JSONObject SnowObject = getInfo.getJSONObject("Snow");
int SnowValue = SnowObject.getInt("Value");
String SnowUnit = SnowObject.getString("Unit");
int SnowUnitType = SnowObject.getInt("UnitType");
// JSON Object (Ice Object)
JSONObject IceObject = getInfo.getJSONObject("Ice");
int IceValue = IceObject.getInt("Value");
String IceUnit = IceObject.getString("Unit");
int IceUnitType = IceObject.getInt("UnitType");
// JSON Object (MobileLink Object)
JSONObject MobileLinkObject = getInfo.getJSONObject("MobileLink");
String MobileLink = MobileLinkObject.getString("MobileLink");
// JSON Object (Link Object)
JSONObject LinkObject = getInfo.getJSONObject("Link");
String Link_value = LinkObject.getString("Link");
System.out.println("Some of the outputs : " + Visibiliyt_value +"\n" + Visibility_Unit +"\n" + DateTimeValue ) ;
}
}catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
new Thread(new Runnable() {
public void run() {
System.out.println("Json parsing error: " + e.getMessage());
}
}).start();
}
}else {
System.out.print("Couldn't get json from server.");
new Thread(new Runnable() {
public void run() {
System.out.println("Couldn't get json from server. Check LogCat for possible errors!");
}
}).start();
}
return null;
}
*JSON example:*
Code:
[
{
"DateTime": "2017-11-29T21:00:00+08:00",
"EpochDateTime": 1511960400,
"WeatherIcon": 35,
"IconPhrase": "Partly cloudy",
"IsDaylight": false,
"Temperature": {
"Value": -5.8,
"Unit": "C",
"UnitType": 17
},
"RealFeelTemperature": {
"Value": -6.7,
"Unit": "C",
"UnitType": 17
},
"WetBulbTemperature": {
"Value": -7.4,
"Unit": "C",
"UnitType": 17
},
"DewPoint": {
"Value": -12,
"Unit": "C",
"UnitType": 17
},
"Wind": {
"Speed": {
"Value": 7.4,
"Unit": "km/h",
"UnitType": 7
},
"Direction": {
"Degrees": 353,
"Localized": "N",
"English": "N"
}
},
"WindGust": {
"Speed": {
"Value": 11.1,
"Unit": "km/h",
"UnitType": 7
}
},
"RelativeHumidity": 62,
"Visibility": {
"Value": 16.1,
"Unit": "km",
"UnitType": 6
},
"Ceiling": {
"Value": 9144,
"Unit": "m",
"UnitType": 5
},
"UVIndex": 0,
"UVIndexText": "Low",
"PrecipitationProbability": 0,
"RainProbability": 0,
"SnowProbability": 0,
"IceProbability": 0,
"TotalLiquid": {
"Value": 0,
"Unit": "mm",
"UnitType": 3
},
"Rain": {
"Value": 0,
"Unit": "mm",
"UnitType": 3
},
"Snow": {
"Value": 0,
"Unit": "cm",
"UnitType": 4
},
"Ice": {
"Value": 0,
"Unit": "mm",
"UnitType": 3
},
"CloudCover": 30,
"MobileLink": "http://m.accuweather.com/en/cn/matouying-town/83987/hourly-weather-forecast/83987?day=1&unit=c&lang=en-us",
"Link": "http://www.accuweather.com/en/cn/matouying-town/83987/hourly-weather-forecast/83987?day=1&hbhhour=21&unit=c&lang=en-us"
},
]
Would love to know what my mistake, been looking at it for a while now
So much more details came up 