Reputation: 273
I am using Volley to retrieve JSON format from a server by the following way:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_device_report);
setContentView(R.layout.activity_fuel);
linechart = (LineChart) findViewById(R.id.lineChart);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray dataset = response.getJSONArray("dataset");
JSONObject petrol = dataset.getJSONObject(0);
JSONObject diesel = dataset.getJSONObject(1);
JSONArray categories = response.getJSONArray("categories");
JSONArray petrolarray = petrol.getJSONArray("data");
JSONArray dieselarray = diesel.getJSONArray("data");
Log.d("debug","Size of PetrolArray: " + petrolarray.length());
for(int i = 0; i<petrolarray.length();i++) {
Log.d("debug", "Petrol Prices consists of: " + petrolarray.getJSONObject(i).getString("value"));
float prtl = Float.parseFloat(petrolarray.getJSONObject(i).getString("value"));
float dsl = Float.parseFloat(dieselarray.getJSONObject(i).getString("value"));
petrolPrices.add(new Entry(i,prtl));
dieselPrices.add(new Entry(i,dsl));
xMonths.add(categories.getJSONObject(i).getString("label"));
}
} catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(req);
ArrayList<ILineDataSet> lineDataSets= new ArrayList<>();
LineDataSet lineDataSet1 = new LineDataSet(petrolPrices,"petrol");
lineDataSets.add(lineDataSet1);
Log.d("debug","Size of PetrolPrices: " + petrolPrices.size());
linechart.setData(new LineData(lineDataSets));
linechart.setVisibleXRangeMaximum(12);
}
When I test this code, the Debug Message with "Size of PetrolPrices" comes up first, and then the "Size of PetrolArray" Debug Messages comes. This means that the PetrolPrices arraylist is empty when it draws the graph causing an error. The data doesn't get added into it until after even though the code is before.
I am not sure if I explained this well but hopefully it is enough. How can I fix this issue?
Upvotes: 0
Views: 111
Reputation: 2239
It is highly recommended to use functions to make sure your code is good for reading and error tracing.
Use code like this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fuel);
linechart = (LineChart) findViewById(R.id.lineChart);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray dataset = response.getJSONArray("dataset");
JSONObject petrol = dataset.getJSONObject(0);
JSONObject diesel = dataset.getJSONObject(1);
JSONArray categories = response.getJSONArray("categories");
JSONArray petrolarray = petrol.getJSONArray("data");
JSONArray dieselarray = diesel.getJSONArray("data");
Log.d("debug","Size of PetrolArray: " + petrolarray.length());
for(int i = 0; i<petrolarray.length();i++) {
try{
Log.d("debug", "Petrol Prices consists of: " + petrolarray.getJSONObject(i).getString("value"));
float prtl = Float.parseFloat(petrolarray.getJSONObject(i).getString("value"));
float dsl = Float.parseFloat(dieselarray.getJSONObject(i).getString("value"));
petrolPrices.add(new Entry(i,prtl));
dieselPrices.add(new Entry(i,dsl));
xMonths.add(categories.getJSONObject(i).getString("label"));
} catch (JSONException e){
e.printStackTrace();
//This is make sure that if some json data is wrongly formatted it then it should not influence other results.
}
}
processResult(petrolPrices);
} catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(req);
}
And create a function to process the result like this.
private void processResult(Arraylist<Entry> petrolPrices)
{
ArrayList<ILineDataSet> lineDataSets= new ArrayList<>();
LineDataSet lineDataSet1 = new LineDataSet(petrolPrices,"petrol");
lineDataSets.add(lineDataSet1);
Log.d("debug","Size of PetrolPrices: " + petrolPrices.size());
linechart.setData(new LineData(lineDataSets));
linechart.setVisibleXRangeMaximum(12);
}
Upvotes: 1
Reputation: 1536
this has to do with the asynchronous request that you're making to get the JSON data. Since this section of code has to wait for a server response, it continues on with the code below the request block. To fix it, just move inside of the request function.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_device_report);
setContentView(R.layout.activity_fuel);
linechart = (LineChart) findViewById(R.id.lineChart);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray dataset = response.getJSONArray("dataset");
JSONObject petrol = dataset.getJSONObject(0);
JSONObject diesel = dataset.getJSONObject(1);
JSONArray categories = response.getJSONArray("categories");
JSONArray petrolarray = petrol.getJSONArray("data");
JSONArray dieselarray = diesel.getJSONArray("data");
Log.d("debug","Size of PetrolArray: " + petrolarray.length());
for(int i = 0; i<petrolarray.length();i++) {
Log.d("debug", "Petrol Prices consists of: " + petrolarray.getJSONObject(i).getString("value"));
float prtl = Float.parseFloat(petrolarray.getJSONObject(i).getString("value"));
float dsl = Float.parseFloat(dieselarray.getJSONObject(i).getString("value"));
petrolPrices.add(new Entry(i,prtl));
dieselPrices.add(new Entry(i,dsl));
xMonths.add(categories.getJSONObject(i).getString("label"));
}
ArrayList<ILineDataSet> lineDataSets= new ArrayList<>();
LineDataSet lineDataSet1 = new LineDataSet(petrolPrices,"petrol");
lineDataSets.add(lineDataSet1);
Log.d("debug","Size of PetrolPrices: " + petrolPrices.size());
// linechart.setData(new LineData(lineDataSets));
linechart.setVisibleXRangeMaximum(12);
} catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(req);
}
Upvotes: 1