3 回答
TA贡献1853条经验 获得超6个赞
AsyncTask 用于执行后台操作并在 UI 线程上发布结果。在您的情况下,您应该放入eins.setTextAsyncTask onPostExecute。
另一个问题是因为 AsyncTask 是一个单独的类,所以您需要定义一个接口将结果传递回 MainActivity。
消息
public class News extends AsyncTask<String, Void, String> {
private WeakReference<OnNewsListener> mOnNewsListener;
public void setOnNewsListener(OnNewsListener listener) {
mOnNewsListener = new WeakReference<>(listener);
}
@Override
protected String doInBackground(String... strings) {
final StringBuilder builder = new StringBuilder();
final StringBuilder builder2 = new StringBuilder();
{
try {
Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();
String title = doc.title();
Elements links = doc.select("h2");
Elements links2 = doc.select("h3");
builder.append(title).append("\n");
for (Element link : links) {
builder.append(link.text()).append("$");
}
for (Element link : links2) {
builder2.append(link.text()).append("$");
}
} catch (IOException e) {
e.printStackTrace();
}
}
String text = builder.toString() + "%" + builder2.toString();
return text;
}
@Override
protected void onPostExecute(String text) {
if (mOnNewsListener != null) {
if (mOnNewsListener.get() != null) {
mOnNewsListener.get().onNews(text);
}
}
}
public interface OnNewsListener {
void onNews(String text);
}
}
主要活动
public class MainActivity extends AppCompatActivity implements News.OnNewsListener{
TextView eins;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eins = findViewById(R.id.eins);
}
@Override
protected void onResume() {
super.onResume();
News news = new News();
news.setOnNewsListener(this);
news.execute();
}
@Override
public void onNews(String text) {
eins.setText(text);
}
}
TA贡献1993条经验 获得超5个赞
根据pz64的建议,在方法中设置文本onPostExecute()并调用AsyncTask()不调用get()方法。get()AsyncTask 上的方法使任务同步并影响您的 UI。
public class News extends AsyncTask<String, Void, String> {
@override
protected void onPreExecute(){
//initiate your loading views
}
@Override
protected String doInBackground(String... strings) {
final StringBuilder builder = new StringBuilder();
final StringBuilder builder2 = new StringBuilder();
{
try {
Document doc = Jsoup.connect("http://www.schwimmclub-schwandorf.de/index.php/8-home/56-infos-neuigkeiten").get();
String title = doc.title();
Elements links = doc.select("h2");
Elements links2 = doc.select("h3");
builder.append(title).append("\n");
for (Element link : links) {
builder.append(link.text()).append("$");
}
for (Element link : links2) {
builder2.append(link.text()).append("$");
}
} catch (IOException e) {
e.printStackTrace();
}
}
String text = builder.toString() + "%" + builder2.toString();
return text;
}
@override
protected void onPostExecute(String response){
//dispose loading views
if(response != null){
eins.setText(response);
}else{
//could not load
}
}
}
称呼:
@Override
protected void onResume()
{
super.onResume();
new News().execute(); //do not call get method
}
TA贡献1806条经验 获得超5个赞
您可以在 oncreate 方法中调用 asyncTask。并在 onProgressUpdate 方法中设置结果。
@Override
protected void onProgressUpdate(String... text) {
eins.setText.setText(text);
}
添加回答
举报