AsyncTask的postExecute方法无法正常工作

时间:2011-12-20 06:13:53

标签: java android graph android-asynctask

如果我在单独的项目中运行上下代码,它就可以运行。但是当我尝试集成它们时,图表就不会被填充。

我觉得在postExecute方法中的原因是

需要注意的一件重要事情是当我运行任何其他随机项目时,对其进行一些更改,然后重新启动应用程序一段时间 Graph.java 是可见的因为它在背景中。那时,输出完美无缺。

但如果我运行 Graph.java ,它会显示空白图。

修改

{{第一个代码是从URL获取数据并将其存储在各种数组中。

第二个代码是在图表上显示数据。

在下面的代码中,传递给图形的两个数组包含静态值,它们应该从第一个代码中获取值。 }}

请帮助

Graph.java

  public class Graph extends Activity {

    String arr[], arr1[], temp[], company[], current[], close[], time[];
    int sub;
    String s1;
    String[] verlabels = new String[] { " y1 ", " y2 ", " y3 " };
    String[] horlabels = new String[] { " x1 ", " x2 ", " x3 ", " x4 " };
    private XYPlot mySimpleXYPlot;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://abc.com/stockcharts.aspx?id=Reliance" });

    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();
                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {

            sub = result.lastIndexOf('@', result.length());
            s1 = result.substring(0, sub + 2);
            arr1 = s1.split("@");
            arr = new String[arr1.length - 1];
            company = new String[arr.length];
            current = new String[arr.length];
            close = new String[arr.length];
            time = new String[arr.length];

            for (int j = 0; j < arr.length; j++) {
                arr[j] = arr1[j];
            }

            for (int i = 0; i < arr.length; i++) {
                temp = arr[i].split(";");
                company[i] = temp[0];
                current[i] = temp[1];
                close[i] = temp[2];
                time[i] = temp[3];
            }

mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
                Number[] series1Numbers = { 1324266358000L, 1324266418000L,
                        1324266478000L, 1324266538000L, 1324266598000L,
                        1324266658000L, 1324266718000L, 1324266778000L,
                        1324266838000L, 1324266898000L, 1324266958000L };
                Number[] series2Numbers = { 3, 1, 2, 9, 6, 5, 4, 3, 7, 3, 2 };

            XYSeries series1 = new SimpleXYSeries(
                    Arrays.asList(series1Numbers),
                    Arrays.asList(series2Numbers),
                    "Company");

            LineAndPointFormatter series1Format = new LineAndPointFormatter(
                    Color.rgb(0, 200, 0), // line color
                    Color.rgb(0, 100, 0), // point color
                    Color.rgb(150, 190, 150)); // fill color (optional)

            mySimpleXYPlot.addSeries(series1, series1Format);
            mySimpleXYPlot.setTicksPerRangeLabel(2);
            mySimpleXYPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);

            mySimpleXYPlot.setDomainValueFormat(new MyDateFormat());
            mySimpleXYPlot.disableAllMarkup();

        }
        public class MyDateFormat extends Format {
            private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");

            @Override
            public StringBuffer format(Object obj, StringBuffer toAppendTo,
                    FieldPosition pos) {
                long timestamp = ((Number) obj).longValue();
                Date date = new Date(timestamp);
                return dateFormat.format(date, toAppendTo, pos);
            }
            @Override
            public Object parseObject(String source, ParsePosition pos) {
                return null;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我创建了另一个用于绘制图形的类。

使用intent从当前类调用该类。使用intent属性将两个数组传递给图类。使用两个数组在第二个活动中绘制图表。