GAMA
GAMA

Reputation: 5996

the postExecute method of AsyncTask not working properly

If I run above and below code in separate projects, it works. But when I try integrate them, the graph is not getting populated.

The reason I feel is in postExecute method because

One important thing to notice is when I run any other random project, make some change in it, and relaunch the app then for some moments Graph.java is visible as it is in background. At that moment, output is coming perfectly.

But if I run the Graph.java , it's showing blank graph.

EDIT

{{ 1st code is to fetch data from URL and store it in various arrays.

2nd code is to diaplay data on graph.

In below code the two arrays passed to graph contain static values, they should take the values from the 1st code. }}

PLEASE HELP

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;
            }
        }
    }
}

Upvotes: 2

Views: 480

Answers (1)

Krunal
Krunal

Reputation: 6490

I created another class for plotting graph.

Call that class from current class by using intent. Passed the two arrays to graph class by using intent property. plotted the graph in 2nd activity using two arrays.

Upvotes: 1

Related Questions