Asynctask中的其他部分永远不会被执行

时间:2017-03-01 05:47:37

标签: android android-asynctask jsoup

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText movieText = (EditText) findViewById(R.id.movieText);
    txt = movieText.getText().toString();
    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new crawling().execute();

        }
    });
}

public class crawling extends AsyncTask<Void, Void, Void> {

    String url ="http://www.filmibeat.com/movie_listings/Chennai.html";
    Document doc;
    Elements linksOnPage;

    @Override
    protected Void doInBackground(Void... params) {
        try {
            doc = Jsoup.connect(url).get();
            linksOnPage = doc.select("a[href]");
        }
        catch (Exception e){e.printStackTrace();}
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        for(Element link : linksOnPage) {
            if (link.tagName("title").toString().contains(txt)) {
                final Toast toast = Toast.makeText(MainActivity.this,"YO",Toast.LENGTH_SHORT);
                toast.show();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toast.cancel();
                    }
                },1000);

            }
            else{
                final Toast toast = Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_SHORT);
                toast.show();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toast.cancel();
                    }
                },1000);
            }
        }
        super.onPostExecute(aVoid);
    }
}

无论我在Edittext中提供什么,无论是字符串还是什么,我只能得到一个&#34; YO&#34;敬酒。 else部分永远不会被执行为真正的第二种情况。为什么?请帮忙。我是否正确解析了链接标题?

1 个答案:

答案 0 :(得分:0)

在开始AsyncTask之前从EditText onClick of Button获取输入:

public void onClick(View v) {
            // get here text from EditText
           txt = movieText.getText().toString();
            new crawling().execute();

        }