最近我正在开发一个我被分配到的应用程序。
我制作了一个文本视图来显示一些数据,这些数据是从我的raspberry pi的JSON url加载的(我知道这很愚蠢,但我不在乎)。
现在我想让同样的数据显示在一个叫做“条形图”的东西里。这是一个由mikephil制造的已知api(如果你想知道的话,“https://github.com/PhilJay/MPAndroidChart”)。
我好像不能让它工作。我让应用程序运行,它可以显示图表,但它没有填充数据,它保持在0.0。。。
现在我的问题来了:有人能告诉我我做错了什么吗?
public class grafiek extends Activity {
//public static final String JSON_URL = "http://myip/test.php";
//public static String agv = "";
//public static String binnenschip = "";
/*public static String vrachtwagen;
public static String zeeschip;
public static String opslag;
public static String trein;
public static String diversen;*/
public static int agvz;
public static int binns;
/*public static int vracht;
public static int zschip;
public static int opsl;
public static int trei;
public static int divers;*/
//String aantalAgv = MainActivity.agv;
//String aantalBinnenschip = MainActivity.binnenschip;
//String aantalVrachtwagen=MainActivity.vrachtwagen;
//String aantalZeeschip=MainActivity.zeeschip;
//String aantalOpslag=MainActivity.opslag;
//String aantalTrein=MainActivity.trein;
//String aantalDiversen=MainActivity.diversen;
int counter = 0;
String loginURL="http://myip/test.php";
String datas = "";
RequestQueue requestQueue;
BarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grafiek);
if(savedInstanceState!=null){
Log.d("STATE", savedInstanceState.toString());
}
requestQueue = Volley.newRequestQueue(this);
chart = (BarChart) findViewById(R.id.chart);
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, loginURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray ja = response.getJSONArray("result");
for(int i=0; i < ja.length(); i++){
JSONObject jsonObject = ja.getJSONObject(i);
// int id = Integer.parseInt(jsonObject.optString("id").toString());
String agv = jsonObject.getString("agv");
String binnenschip = jsonObject.getString("binnenschip");
agvz = Integer.parseInt(agv);
binns = Integer.parseInt(binnenschip);
}
}
catch(JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley","Error");
}
}
);
requestQueue.add(jor);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(agvz, 0));
entries.add(new BarEntry(binns, 1));
BarDataSet dataset = new BarDataSet(entries, "Aantal containers");
ArrayList<String> labels = new ArrayList<String>();
labels.add("AGV");
labels.add("Binnenschip");
BarChart chart = new BarChart(this);
setContentView(chart);
BarData data = new BarData(labels, dataset);
chart.setData(data);
chart.setDescription("Aantal containers per platform");
dataset.setColors(ColorTemplate.COLORFUL_COLORS);
if(counter == 0) {
chart.animateY(2500);
counter++;
}
}
如果有人能帮我,那就太好了!
最佳答案
因为截击是异步的,所以您应该如下更新代码(请注意// MOVE HERE...
):
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, loginURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray ja = response.getJSONArray("result");
for(int i=0; i < ja.length(); i++){
JSONObject jsonObject = ja.getJSONObject(i);
// int id = Integer.parseInt(jsonObject.optString("id").toString());
String agv = jsonObject.getString("agv");
String binnenschip = jsonObject.getString("binnenschip");
agvz = Integer.parseInt(agv);
binns = Integer.parseInt(binnenschip);
// MOVE HERE...
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(agvz, 0));
entries.add(new BarEntry(binns, 1));
BarDataSet dataset = new BarDataSet(entries, "Aantal containers");
ArrayList<String> labels = new ArrayList<String>();
labels.add("AGV");
labels.add("Binnenschip");
BarChart chart = new BarChart(this);
setContentView(chart);
BarData data = new BarData(labels, dataset);
chart.setData(data);
chart.setDescription("Aantal containers per platform");
dataset.setColors(ColorTemplate.COLORFUL_COLORS);
if(counter == 0) {
chart.animateY(2500);
counter++;
}
}
}
catch(JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
requestQueue.add(jor);