Android - How to Represent JSON Information from URL through GSON [SOLVED]
Clash Royale CLAN TAG#URR8PPP
Android - How to Represent JSON Information from URL through GSON [SOLVED]
I have been attempting to retrieve information from an online JSON Source using a URL. I initially got the information parsed to a TextView in Android, however I got ALL Information from the JSON. Below is a screenshot of what I am referring to.
Text View Showing All Data In Json
Essentially, What I want is to just show the Location, so in the above example, Oslo, in Norway in one Textview, then in another show the Current Moon Phase for that location, so - using the above example again, Waxing Crescent.
This is the JSON I am using.
"version": 2,
"locations": [
"id": "187",
"geo":
"name": "Oslo",
"country":
"id": "no",
"name": "Norway"
,
"latitude": 59.913,
"longitude": 10.740
,
"astronomy":
"objects": [
"name": "moon",
"days": [
"date": "2018-09-13",
"events": ,
"moonphase": "waxingcrescent"
]
]
]
To parse the JSON to the Textview showing all the data I used the following.
JSONObject jo = new JSONObject(data);
JSONArray locations = jo.getJSONArray("locations");
for (int i = 0; i < locations.length(); ++i)
JSONObject loc = locations.getJSONObject(0);
JSONObject geo = loc.getJSONObject("geo");
JSONObject country = geo.getJSONObject("country");
JSONObject astronomy = loc.getJSONObject("astronomy");
JSONObject objects = astronomy.getJSONArray("objects").getJSONObject(0);
JSONObject day = objects.getJSONArray("days").getJSONObject(0);
StringBuilder result = new StringBuilder();
String singleParsed = "Moon: "+ astronomy.getString("moonphase");
} catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (JSONException e)
e.printStackTrace();
return null;
}
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
@Override
protected void onPostExecute(Void aVoid)
super.onPostExecute(aVoid);
MainActivity.fetcheddata.setText(this.data);
NOTE: A String is created in the For Loop, Called "Single Parsed", This is what I want to put on the screen, however - once I call that variable, nothing shows on the screen. It only shows when using:
MainActivity.fetcheddata.setText(this.data);
But Not with the following, I do actually receive an error saying "Cannot Resolve Symbol, singleParsed as well.
MainActivity.fetcheddata.setText(this.singleParsed);
After some research and previous questions on Stack, I believe GSON is the way to go to do what I wish, however I have no idea where to start, or how to do the task using GSON.
All help is appreciated.
Secondly,
MainActivity.fetcheddata
is just wrong. Never make a View a static variable. Whatever networking library you are using needs to update the UI itself.– cricket_007
1 hour ago
MainActivity.fetcheddata
1 Answer
1
JSON Information from URL through GSON
Simple answer - Look at using Retrofit, and find a tutorial on it, as well as Gson
However
You don't need Gson to fix your problem, and I suggest fixing the problem at hand rather than going down an unnecessary path.
First, singleParsed
is a local variable within a loop. Therefore, this.singleParsed
doesn't exist. Or if it does, you never set it.
singleParsed
this.singleParsed
Secondly, let's assume you only have one location, so you don't need a loop. Similar to how you accessed the other JSON arrays, just get the first array item.
Finally, you'll need to understand how AsyncTask works, but my general recommended solution is at How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
But, for this purpose, here is a simple example with AsyncTask<String, Void, String>
AsyncTask<String, Void, String>
// protected String doInBackground(String... url) // I assume you have this ??
// String data = parseJsonFrom(url[0]);
String singleParsed = "(none)";
try
JSONObject jo = new JSONObject(data);
JSONObject firstLocationAstronomy = jo
.getJSONArray("locations").getJSONObject(0)
.getJSONObject("astronomy");
JSONObject firstDay = firstLocationAstronomy
.getJSONArray("objects").getJSONObject(0)
.getJSONArray("days").getJSONObject(0);
singleParsed = firstDay.getString("moonphase");
catch (Exception e) // TODO: catch a JSONParseException
return singleParsed; // This is returned to onPostExecute, don't store a variable elsewhere
Then in the MainActivity, wherever you make this task, and set the text
new MoonPhaseTask()
@Override
protected void onPostExecute(String moonPhase)
MainActivity.this.textView.setText("Phase: " + moonPhase);
.execute("some url");
What I want is to just show the Location, so in the above example, Oslo, in Norway in one Textview, then in another show the Current Moon Phase for that location
You can change the AsyncTask
to return you the entire JSONObject to the onPostExecute
, and parse the JSON there if you really wanted. Just the networking pieces need to be off the UI thread... parsing is not computationally expensive.
AsyncTask
onPostExecute
Can you inform me of why I receive "Cannot Resolve Method "getJSONObject(java.lang.String)", on ".getJSONObject("geo")"? Many thanks.
– KeptAwakeByCoffee
1 hour ago
Probably because I typo-ed something and this code isn't exactly meant to be copy-pasted exactly as-is
– cricket_007
56 mins ago
Of course, no code should be directly pasted.I attempted to type it out and debugging, however no luck so far. Thanks anyway.
– KeptAwakeByCoffee
55 mins ago
I updated the answer with the corrected method
– cricket_007
55 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It would help if you read the Gson User Guide first, then try the examples... Feel free to edit your question when done with that
– cricket_007
1 hour ago