CelestialCreator CelestialCreator - 27 days ago 8
JSON Question

How so I return multiple JSON values to multiple text views?

I did research for

JSON
. But only found tutorials for
JSONArray
, not how to make allow multiple text views for a
JSONObject
. Should I have another method, add parameters or what?

This is my
JSON
,

{
"name": "iss",
"id": 25544,
"latitude": -16.621580566205,
"longitude": -22.573425077475,
"altitude": 403.33965999684,
"velocity": 27615.932538565,
"visibility": "daylight",
"footprint": 4421.7128198729,
"timestamp": 1478617317,
"daynum": 2457701.1263542,
"solar_lat": -16.808913358406,
"solar_lon": 310.46294067763,
"units": "kilometers"
}


Here is the code if you want to see it,

public class IsstatusActivity extends AppCompatActivity {
String longitSt;
JSONParser jsonparser = new JSONParser();
TextView latitude;
TextView longitude;
String lat;
String longit;
JSONObject jobj = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.isstatus);
latitude = (TextView) findViewById(R.id.Coordinates);

new retrievedata().execute();
}

class retrievedata extends AsyncTask<String, String, String> {

@Override
public String doInBackground(String... args) {
jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544");
try {
lat = "latitude: " + jobj.getString("latitude");
longit = "longitude :" + jobj.getString("longitude");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lat;
}

@Override
protected void onPostExecute(String lat) {
latitude.setText(lat);
longit.setText(longitude);
}
}
}

Answer

You are doing it wrong
In onPostExecute() instead of longit.setText(longitude) use longitude.setText(longit) as longitude is a TextView reference and longit is a String.It goes like this
1. Use String lat and long as global variables.
2. After making network call set lat and long values.
3. In onPostExecute setText for your textviews.

Comments