Aexyn
Aexyn

Reputation: 1222

AsyncTask's doInBackground is not working exactly after I call it

Here are my three activities, the problem is I am getting null as output of Xstring in LineGraph. When I debug it, it shows pattern like this:

parseStrings = mJsoupAct.getOutput(); LineGraph.java
mExecute(); jsoupAct.java
new Parsee().execute(); jsoupAct.java
Log.d("xoutputD", output + ""); jsoupAct.java
url = "http://www.google.co.in/"; jsoupAct.java ...........

Now the problem is.. Why 4th line is executing before 5th. Due to this execution, it return null and then executes doInBackground. why this is happening???

MainActivity

public class MainActivity extends Activity {
jsoupAct mJsoupAct = new jsoupAct();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("xmain", "main");

    Intent i = new Intent(getApplicationContext(), LineGraph.class);
    startActivity(i);
}
}

LineGraph

public class LineGraph extends Activity {
String parseStrings;
jsoupAct mJsoupAct;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mJsoupAct = new jsoupAct();
    Xparse();
}
public void Xparse() {
    parseStrings = mJsoupAct.getOutput();
    Log.d("xstring", parseStrings + "");
}
}

jsoupAct

public class jsoupAct extends Activity {
public String output;
public String url;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

public void mExecute() {
    new Parsee().execute();

}

public class Parsee extends AsyncTask<String, String, String> {

    protected String doInBackground(String... params) {
        try {
            url = "http://www.google.co.in/";
            Document doc = Jsoup.connect(url).get();
            String body = doc.body().text();
            output = body.toString();
            Log.d("xoutputD0", output + "");
        } catch (IOException e) {

            e.printStackTrace();
        }
        return output;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(output);
        Log.d("xoutputD1", output + "");
    }
}

public String getOutput() {
    mExecute();
    Log.d("xoutputD", output + "");
    return output;
}

public void setOutput(String output) {
    this.output = output;
}
}

Upvotes: 0

Views: 162

Answers (2)

Aexyn
Aexyn

Reputation: 1222

I have found out the way. Actually Jsoup is taking much more time in thread in parallel that main thread is not waiting for that and keep running and hence returns null as output. So to overcome this, I added sleep to main thread in JsoutAct.java and whhooooppp.. Its done

public String getOutput() {
        mExecute();
        Log.d("xoutputD2", output + "");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return output;
    }

Upvotes: 2

Yordan Lyubenov
Yordan Lyubenov

Reputation: 191

As far as I can see, you're misunderstanding what AsyncTask does. It works in the background asynchronously, so it does not wait for the next line of your code right below it. If you want to retrieve something from doInBackground, use onPostExecute. That can be overriden in your class Parsee. See, it will fire when doInBackground is finished. Moreover, onPostExecute is executed on the main thread (UI), so you can do whatever you want to from the UI from there.

And, just a friendly advice - get one class with AsyncTask and call that instance from onCreate. It was really hard to follow your code, and I do not think it is necessary to overcomplicate things (unless you really have to?)

P.S. I am bad at explanations, so ask away if I did a poor job.

Upvotes: 0

Related Questions