2015-01-09 72 views
0

如何在使用JSoup解析器的示例中应用AsyncTasc?只是我尝试使用线程,但是这不起作用,从Android版本4.x.x开始。也许有人知道,下面的例子是如何添加AsyncTasc? 感谢您的提前。jsoup解析器中的AsyncTasc?

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class JSoupStudyActivity extends Activity { 

// blog url 
static final String BLOG_URL = "http://xjaphx.wordpress.com/"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // set layout view 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // process 
    try { 
     ((TextView)findViewById(R.id.tv)).setText(getBlogStats()); 
    } catch (Exception ex) { 
     ((TextView)findViewById(R.id.tv)).setText("Error"); 
    } 
} 

protected String getBlogStats() throws Exception { 
    String result = ""; 
    // get html document structure 
    Document document = Jsoup.connect(BLOG_URL).get(); 
    // selector query 
    Elements nodeBlogStats = document.select("div#blog-stats ul li"); 
    // check results 
    if(nodeBlogStats.size() > 0) { 
     // get value 
     result = nodeBlogStats.get(0).text(); 
    } 

    // return 
    return result; 
    } 
} 

回答

0

这里有一个简单的例子:

public class JSoupStudyTask extends AsyncTask<URL, Integer, Long> // Check doc for explanation 
{ 
    @Override 
    protected Long doInBackground(URL... urls) 
    { 
      // Do your html parsing here 
      // return your result or whatever 
    } 
} 

有一个很好的例子代码在这里:

另外检查AsyncTask documentation,它没有jsoup的例子,但只包含你需要知道让它运行的一切。