JSON 任务后忽略 AndroidPlot 指令
AndroidPlot instructions are ignored after JSON task
(抱歉放了太多代码,但如果我不放,事情会更难解释)
不管怎样,我做了一个指令从 mysql 数据库中获取一些数据:
//asynctask
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("fro", fr));
nameValuePairs.add(new BasicNameValuePair("too",to));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("energystatistic");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String kwh = jsonChildNode.optString("kwh");
s1 = addElement(s1, Integer.parseInt(kwh));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(Plot.this, "Begin plot: " + String.valueOf(s1.length) + " Entries", Toast.LENGTH_SHORT).show();
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(Arrays.asList(s1), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"Series1");
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
// reduce the number of range labels
plot.setTicksPerRangeLabel(3);
plot.getGraphWidget().setDomainLabelOrientation(-45);
Toast.makeText(Plot.this, "Plotted", Toast.LENGTH_SHORT).show();
}
}
// end async task
这里的问题是下面的代码被忽略了或什么的:
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(Arrays.asList(s1),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"Series1");
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
// reduce the number of range labels
plot.setTicksPerRangeLabel(3);
plot.getGraphWidget().setDomainLabelOrientation(-45);
我 运行 一个调试器来检查这些行,程序会 运行 它们,但图表似乎没有受到影响。该图保持空白,我不明白为什么。我需要 运行 使用不同的方法吗?
如果我把它放在 onCreate 方法中,图表会显示一些变化,但仍然根本没有绘制这些点。通过JSon(所有整数)从数据库获取信息后,数组s1[]确实有数据。
修改完绘图后尝试调用 plot.redraw()
。
(抱歉放了太多代码,但如果我不放,事情会更难解释) 不管怎样,我做了一个指令从 mysql 数据库中获取一些数据:
//asynctask
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("fro", fr));
nameValuePairs.add(new BasicNameValuePair("too",to));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("energystatistic");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String kwh = jsonChildNode.optString("kwh");
s1 = addElement(s1, Integer.parseInt(kwh));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(Plot.this, "Begin plot: " + String.valueOf(s1.length) + " Entries", Toast.LENGTH_SHORT).show();
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(Arrays.asList(s1), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"Series1");
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
// reduce the number of range labels
plot.setTicksPerRangeLabel(3);
plot.getGraphWidget().setDomainLabelOrientation(-45);
Toast.makeText(Plot.this, "Plotted", Toast.LENGTH_SHORT).show();
}
}
// end async task
这里的问题是下面的代码被忽略了或什么的:
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(Arrays.asList(s1),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,"Series1");
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
// reduce the number of range labels
plot.setTicksPerRangeLabel(3);
plot.getGraphWidget().setDomainLabelOrientation(-45);
我 运行 一个调试器来检查这些行,程序会 运行 它们,但图表似乎没有受到影响。该图保持空白,我不明白为什么。我需要 运行 使用不同的方法吗?
如果我把它放在 onCreate 方法中,图表会显示一些变化,但仍然根本没有绘制这些点。通过JSon(所有整数)从数据库获取信息后,数组s1[]确实有数据。
修改完绘图后尝试调用 plot.redraw()
。