3 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
AsyncTask 用于執(zhí)行后臺(tái)操作并在 UI 線程上發(fā)布結(jié)果。在您的情況下,您應(yīng)該放入eins.setTextAsyncTask onPostExecute。
另一個(gè)問題是因?yàn)?AsyncTask 是一個(gè)單獨(dú)的類,所以您需要定義一個(gè)接口將結(jié)果傳遞回 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);
}
}
主要活動(dòng)
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貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
根據(jù)pz64的建議,在方法中設(shè)置文本onPostExecute()并調(diào)用AsyncTask()不調(diào)用get()方法。get()AsyncTask 上的方法使任務(wù)同步并影響您的 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貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以在 oncreate 方法中調(diào)用 asyncTask。并在 onProgressUpdate 方法中設(shè)置結(jié)果。
@Override
protected void onProgressUpdate(String... text) {
eins.setText.setText(text);
}
添加回答
舉報(bào)