-4

有人可以帮我在这段代码中实现 AsynkTask 吗?

我是android新手,我真的不知道如何构造代码

public class DisplayActivity extends ListActivity {

private final String TAG="links";

private ArrayAdapter<String> mAdapter = null;
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;

String sites[]={"http://www.dn.pt/inicio",
        "http://www.expresso.sapo.pt","http://www.publico.pt/?fullsite=true"};



@SuppressLint( "NewApi")
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // Show the Up button in the action bar.
    //  getActionBar().setDisplayHomeAsUpEnabled(true);
    //}

    final Intent intent = getIntent();
    final String url_link =intent.getStringExtra(MainActivity.EXTRA_INPUTURL);
    final String keyword = intent.getStringExtra(MainActivity.EXTRA_INPUTKEY);


    mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1);
    final ListView list=(ListView) findViewById(android.R.id.list);
    list.setAdapter(mAdapter);

    //get the juice
    keyword.toLowerCase();


    for(int i=0; i < sites.length;i++){
        Elements elem= getHtmlCode(sites[i]);
        for(Element link:elem){
            if(/*link.absUrl(sites[i]).contains(link.attr("abs:href")) &&*/ link.text().toLowerCase().contains(keyword)){
                    mAdapter.add(link.text() + ": " + link.attr("abs:href")); 
                    mAdapter.notifyDataSetChanged();
            }
        }
    }
}


private Elements getHtmlCode(String url) {
    try {
        if (!url.startsWith("http://") && !url.startsWith("https://"))
            url = "http://" + url;

        Document doc = Jsoup.connect(url).get();
        Elements content=doc.select("a[href]");
        return content;
    } 
    catch (IOException e) {

        // Never e.printStackTrace(), it cuts off after some lines and you'll
        // lose information that's very useful for debugging. Always use proper
        // logging, like Android's Log class, check out
        // http://developer.android.com/tools/debugging/debugging-log.html
        Log.e(TAG, "Failed to load HTML code", e);
        // Also tell the user that something went wrong (keep it simple,
        // no stacktraces):

        Toast.makeText(this, "Failed to load HTML code",
                Toast.LENGTH_SHORT).show(); 
    }
    return null;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
4

1 回答 1

1

有很多关于这个主题的文档。你可以试试这样的

   private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
        protected void onPreExecute (){
            dialog = ProgressDialog.show(YourActivity.this ,"title","message");
        }

         protected void doInBackground(URL... urls) {
            //Add the function that you want to call in an async task here
            for(int i=0; i < urls.length;i++){
                Elements elem= getHtmlCode(urls[i]);
                for(Element link:elem){
                    if(/*link.absUrl(sites[i]).contains(link.attr("abs:href")) &&*/ link.text().toLowerCase().contains(keyword)){
                            mAdapter.add(link.text() + ": " + link.attr("abs:href")); 
                            mAdapter.notifyDataSetChanged();
                    }
                }
            }
         }


         protected void onPostExecute(Long result) {
            dialog.dismiss();
         }
     }

然后你可以从任何你需要的地方调用它:

new DownloadFilesTask().execute(url1, url2, url3);
于 2013-08-12T21:05:28.193 回答