我是新手当我连接到服务器以使用来自url的数据来绘制android API 4.4.2中的图形时,我有这个错误。我尝试修复此错误,但我不能告诉我如何解决此错误
这是我的代码
results = new ArrayList<Result>();
new JSONAsyncTask().execute("http://phm.umr-lab.com/getdata1.php");
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("result");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Day = object.getString("Day");
pH = Float.parseFloat(object.getString("pHdata"));
}
Date.add(Day);
ph.add(pH);
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
dialog.cancel();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
private void openChart(){
int[] x = {0,1,2,3,4,5,6,7 };
Float[] bar = ph.toArray(new Float[Date.size()]);
String[] Datr = Date.toArray(new String[Date.size()]);
// Creating an XYSeries for Income
//CategorySeries incomeSeries = new CategorySeries("Income");
XYSeries incomeSeries = new XYSeries("Data");
// Creating an XYSeries for Income
for(int i=0;i<x.length;i++){
incomeSeries.add(i,bar[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("pHdata");
multiRenderer.setXTitle("pH 1-14");
multiRenderer.setYTitle("Date");
multiRenderer.setZoomButtonsVisible(true);
for(int i=0; i< x.length;i++){
multiRenderer.addXTextLabel(i, Datr[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
// Creating an intent to plot bar chart using dataset and multipleRenderer
Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);
// Start Activity
startActivity(intent);
这是我的错误
threadid=1: thread exiting with uncaught exception(group=0x430d4140)
FATAL EXCEPTION: main
Process: in.wptrafficanalyzer.achartenginebarchart, PID: 15991
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at in.wptrafficanalyzer.achartenginebarchart.MainActivity.openChart(MainActivity.java:164)
at in.wptrafficanalyzer.achartenginebarchart.MainActivity.access$0(MainActivity.java:151)
at in.wptrafficanalyzer.achartenginebarchart.MainActivity$1.onClick(MainActivity.java:66)
at android.view.View.performClick(View.java:4487)
at android.view.View$PerformClick.run(View.java:18746)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5077)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的代码中存在一些问题
首先是:
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Day = object.getString("Day");
pH = Float.parseFloat(object.getString("pHdata"));
}
Date.add(Day);
ph.add(pH);
当您按照上面所示的方式执行此操作时,始终只添加json中的最后一个元素。如果要添加json中的所有元素,则必须在循环内部调用add
方法
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Date.add(object.getString("Day"));
ph.add(Float.parseFloat(object.getString("pHdata"));
}
第二个问题是openChart()
方法。你这样做的地方如下:
int[] x = {0,1,2,3,4,5,6,7 };
Float[] bar = ph.toArray(new Float[Date.size()]);
String[] Datr = Date.toArray(new String[Date.size()]);
...
for(int i=0;i<x.length;i++){
incomeSeries.add(i,bar[i]);
}
...
for(int i=0;i<x.length;i++){
multiRenderer.addXTextLabel(i, Datr[i])
}
你永远不会检查x数组与bar
和Datr
数组的长度是否相同或最大 - 这是非常危险的。你应该这样做:
int[] x = {0,1,2,3,4,5,6,7 };
Float[] bar = ph.toArray(new Float[Date.size()]);
String[] Datr = Date.toArray(new String[Date.size()]);
...
int firstSize = Math.min(x.length, bar.length);
for(int i=0;i<firstSize;i++){
incomeSeries.add(i,bar[i]);
}
...
int secondSize = Math.min(x.length, Datr.length);
for(int i=0;i<secondSize;i++){
multiRenderer.addXTextLabel(i, Datr[i])
}
现在你的解决方案将是安全的。