Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Like Sergey, I've found that the included json library <code>org.json.*</code> on Android is far simpler to use than <code>GSON</code>.</p> <p>For example, in your scenario - your JSON parsing code would look like this.</p> <pre><code>String jsonData = readUrl("http://api.geonames.org/weatherIcaoJSON?ICAO=LSZH&amp;username=demo"); JSONObject weatherJSONObject = new JSONObject( jsonData ); try { // Not sure the format of your data, but you would want something like this String clouds = weatherJSONObject.getString("clouds"); } catch (JSONException e) { e.printStackTrace(); } </code></pre> <p>You would also benefit from <code>AsyncTask</code> or <code>Thread</code>. You never want to run long running operations on the UI thread because the UI will appear unresponsive and sluggish.</p> <p>Below is an example on how you can use <code>AsyncTask</code> to achieve your goal. Read more about it <a href="http://developer.android.com/reference/android/os/AsyncTask.html" rel="nofollow">here</a></p> <pre><code>private class FetchJSONDataTask extends AsyncTask&lt;String, Void, JSONObject&gt; { // This gets executed on a background thread protected JSONObject doInBackground(String... params) { String urlString = params[0]; String jsonData = readUrl( urlString ); JSONObject weatherJSONObject = new JSONObject( jsonData ); return weatherJSONObject; } // This gets executed on the UI thread protected void onPostExecute(JSONObject json) { //Your function that takes a json object and populates views setUpViews( json ); } } </code></pre> <p>And to execute your task, you should run this code in your activity. </p> <pre><code>FetchJSONDataTask task = new FetchJSONDataTask(); task.execute( new String[] { "http://api.geonames.org/weatherIcaoJSON?ICAO=LSZH&amp;username=demo" } ); </code></pre> <p>Note: This code is untested, but that should be the general idea.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload