Java AsyncTask和JSOUP配合得很好

Java AsyncTask和JSOUP配合得很好,java,android-asynctask,jsoup,Java,Android Asynctask,Jsoup,我一直在制作一个个人酒吧主题列表,该列表使用JSOUP解析一个网页,用于列出啤酒,更重要的是,这里是最后一次更改日期。这里的问题是,当我加载活动时,解析的内容不会出现,直到我手动干预并将手指放在指向另一个活动的按钮上。 在我的“公共类主屏幕…”中,我的相关代码如下所示: public class MainScreen extends ExpandableListActivity implements OnChildClickListener { ArrayList<St

我一直在制作一个个人酒吧主题列表,该列表使用JSOUP解析一个网页,用于列出啤酒,更重要的是,这里是最后一次更改日期。这里的问题是,当我加载活动时,解析的内容不会出现,直到我手动干预并将手指放在指向另一个活动的按钮上。 在我的“公共类主屏幕…”中,我的相关代码如下所示:

 public class MainScreen extends ExpandableListActivity 
implements    OnChildClickListener { 
    ArrayList<String> mData = new ArrayList<String>(); 
    ListView mListView; ArrayAdapter<String> mAdapter;
    ArrayList<String> ArrayOfBeerNames; 
    ArrayList<Beer> ArrayOfBeers; 
    ExpandableListView expandbleLis;

         @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.taplist);
    expandbleLis = getExpandableListView();    

    expandbleLis.setDividerHeight(2);
    expandbleLis.setGroupIndicator(null);
    expandbleLis.setClickable(true);


    ArrayOfBeerNames = new ArrayList<String>();

    ArrayOfBeers = new ArrayList<Beer>();

    new AsyncDerp().execute();

}

private class AsyncDerp extends AsyncTask<Void, Void, Void>
{
    private ProgressDialog LoginProgressDialog = new ProgressDialog(MainScreen.this);

    protected void onPreExecute()
    {
        LoginProgressDialog.setMessage("Fetching the up-to-date list...");
        LoginProgressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {


        processUpdate();

        return null;
    }

        private void processUpdate() {
    String URL = "http://www.dogfish.com/restaurant/menus/brews-whats-on-tap.htm";
    try {
        Document doc = Jsoup.connect(URL)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
                .get();

        Elements updates = doc.select("p:matches(.+?)+p");            
        for(Element update: updates) {

            String TheUpdateTextWeGot = update.text();

            if((TheUpdateTextWeGot.contains("updated")))
            {
                String updatedate = "Current as of " + TheUpdateTextWeGot.substring(9, 20) + ".";       
            TextView tv = (TextView)findViewById(R.id.tv);
            tv.setText(updatedate);
            }
        }

        if(mData.size() == 0) {
            mData.add("Empty result");
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        mData.clear();
        mData.add("Exception: " + ex.toString());
    }
}

protected void onPostExecute(Void unused)
{

    LoginProgressDialog.dismiss(); 
    }
}
public类主屏幕扩展了ExpandableListActivity
实现OnChildClickListener{
ArrayList mData=新的ArrayList();
ListView mListView;ArrayAdapter mAdapter;
ArrayList ArrayOfBeerNames;
ArrayList ArrayOfBeers;
可扩展列表视图可扩展列表;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.taplist);
expandbleLis=getExpandableListView();
可膨胀的;可膨胀的;可膨胀的;可膨胀的;
expandbleLis.setGroupIndicator(空);
expandbleLis.setClickable(真);
ArrayOfBeerNames=新的ArrayList();
ArrayOfBeers=新的ArrayList();
新建AsyncDerp().execute();
}
私有类AsyncDerp扩展了AsyncTask
{
private ProgressDialog LoginProgressDialog=新建ProgressDialog(主屏幕。此);
受保护的void onPreExecute()
{
setMessage(“获取最新列表…”);
LoginProgressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
processUpdate();
返回null;
}
私有void processUpdate(){
字符串URL=”http://www.dogfish.com/restaurant/menus/brews-whats-on-tap.htm";
试一试{
Document doc=Jsoup.connect(URL)
.userAgent(“Mozilla/5.0(Windows NT 6.1;WOW64;rv:5.0)Gecko/20100101 Firefox/5.0”)
.get();
元素更新=doc.select(“p:matches(+?)+p”);
用于(元素更新:更新){
字符串theupdatetextwogot=update.text();
if((updatetextwogt.contains(“updated”))
{
String updatedate=“截至“+TheUpdateTextWeGot.substring(9,20)+”的当前值”;
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(updatedate);
}
}
如果(mData.size()==0){
mData.add(“空结果”);
}
}捕获(例外情况除外){
例如printStackTrace();
mData.clear();
添加(“异常:+ex.toString());
}
}
受保护的void onPostExecute(未使用的void)
{
LoginProgressDialog.discouse();
}
}
我意识到,正如许多SE帖子提醒我的那样,这里发生了一些事情。它使用Async下载页面并对其进行解析,但应用程序也应该自动显示数据,而无需我干预并强制它这样做

我哪里出了问题


非常感谢。

试试这个

public class MainScreen extends ExpandableListActivity 
implements    OnChildClickListener { 
    ArrayList<String> mData = new ArrayList<String>(); 
    ListView mListView; ArrayAdapter<String> mAdapter;
    ArrayList<String> ArrayOfBeerNames; 
    ArrayList<Beer> ArrayOfBeers; 
    ExpandableListView expandbleList;
    Document doc;
  @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.taplist);
    expandbleLis = getExpandableListView();    

    expandbleLis.setDividerHeight(2);
    expandbleLis.setGroupIndicator(null);
    expandbleLis.setClickable(true);


    ArrayOfBeerNames = new ArrayList<String>();

    ArrayOfBeers = new ArrayList<Beer>();

    new progression().execute();
}


public class progression extends AsyncTask<Void, Void, Void> {
        ProgressDialog dialog = new ProgressDialog(
                MainScreen.this);

        @Override
        protected void onPreExecute() {
            this.dialog.setCancelable(true);
            this.dialog
                    .setMessage("Fetching the up-to-date list..., Please Wait.......");
            this.dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            this.dialog.setProgress(0);
            this.dialog.setMax(1000);
            this.dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            try {
                /*
                 * This is run on a background thread, so we can sleep here or
                 * do whatever we want without blocking UI thread. A more
                 * advanced use would download chunks of fixed size and call
                 * publishProgress();
                 */


   String URL = "http://www.dogfish.com/restaurant/menus/brews-whats-on-tap.htm";
    try {
        doc = Jsoup.connect(URL)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
                .get();



    } catch (Exception ex) {
        ex.printStackTrace();
        //mData.clear();
        //mData.add("Exception: " + ex.toString());
    }


            } catch (Exception e) {
                //Log.e("tag", e.getMessage());
                /*
                 * The task failed
                 */

            }

            /*
             * The task succeeded
             */
            return null;
        }

        protected void onProgressUpdate(Void... params) {
            super.onProgressUpdate(params);

        }

        @Override
        protected void onPostExecute(final Void unused) {
            super.onPostExecute(unused);


            Elements updates = doc.select("p:matches(.+?)+p");            
        for(Element update: updates) {

            String TheUpdateTextWeGot = update.text();

            if((TheUpdateTextWeGot.contains("updated")))
            {
                String updatedate = "Current as of " + TheUpdateTextWeGot.substring(9, 20) + ".";       
            TextView tv = (TextView)findViewById(R.id.tv);
            tv.setText(updatedate);
            }
        }

        if(mData.size() == 0) {
            mData.add("Empty result");
        }

            if (this.dialog.isShowing()) {

                this.dialog.dismiss();

            }
            /*
             * Update here your view objects with content from download. It is
             * save to dismiss dialogs, update views, etc., since we are working
             * on UI thread.
             */

        }
    }
public类主屏幕扩展了ExpandableListActivity
实现OnChildClickListener{
ArrayList mData=新的ArrayList();
ListView mListView;ArrayAdapter mAdapter;
ArrayList ArrayOfBeerNames;
ArrayList ArrayOfBeers;
可扩展列表查看可扩展列表;
文件文件;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.taplist);
expandbleLis=getExpandableListView();
可膨胀的;可膨胀的;可膨胀的;可膨胀的;
expandbleLis.setGroupIndicator(空);
expandbleLis.setClickable(真);
ArrayOfBeerNames=新的ArrayList();
ArrayOfBeers=新的ArrayList();
newprogression().execute();
}
公共类进程扩展异步任务{
ProgressDialog=新建ProgressDialog(
主屏幕;
@凌驾
受保护的void onPreExecute(){
this.dialog.setCancelable(true);
这是一个对话框
.setMessage(“正在获取最新列表…,请稍候…);
this.dialog.setProgressStyle(ProgressDialog.STYLE_微调器);
this.dialog.setProgress(0);
this.dialog.setMax(1000);
this.dialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
/*
*这是在后台线程上运行的,所以我们可以在这里或
*在不阻塞UI线程的情况下做我们想做的任何事情
*高级使用将下载固定大小的块并调用
*出版进度();
*/
字符串URL=”http://www.dogfish.com/restaurant/menus/brews-whats-on-tap.htm";
试一试{
doc=Jsoup.connect(URL)
.userAgent(“Mozilla/5.0(Windows NT 6.1;WOW64;rv:5.0)Gecko/20100101 Firefox/5.0”)
.get();
}捕获(例外情况除外){
例如printStackTrace();
//mData.clear();
//添加(“异常:+ex.toString());
}
}捕获(例外e){
//Log.e(“tag”,e.getMessage());
/*
*任务失败了
*/
}
/*
*任务成功了
*/
返回null;
}
受保护的void onProgressUpdate(void…参数){
super.onProgressUpdate(参数);
}
@凌驾
后期执行时受保护的无效(最终无效未使用){
super.onPostExecute(未使用);
元素更新=doc.select(“p:matches(+?)+p”);
用于(元素更新:更新){
字符串theupdatetextwogot=update.text();
if((updatetextwogt.contains(“updated”))
{
String updatedate=“截至“+TheUpdateTextWeGot.substring(9,20)+”的当前值”;
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(updatedate);
}
}