小编典典

AsyncTask和doInBackground错误

java

我遵循了很多我认为确实正确的指南和其他问题,但是我有一个无法解决的错误。

反对这条线

private class loadNotams extends AsyncTask<String, Void, Void> {

我收到此错误

MainActivity.loadNotams类型必须实现继承的抽象方法AsyncTask.doInBackground(String …)

并反对这条线

protected Void doInBackground(String airfield) {

我收到此错误:

MainActivity.loadNotams类型的方法doInBackground(String)必须重写或实现一个超类型方法

这里的任何想法都是我的整个代码:

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView testText;
Notam[] notamList = new Notam[100];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Document doc = null;

    testText = (TextView) findViewById(R.id.textview);

    new loadNotams().execute("ybmk");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class loadNotams extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String airfield) {
        Document doc = null;

        try {
            doc = Jsoup
                    .connect(
                            "https://pilotweb.nas.faa.gov/PilotWeb/notamRetrievalByICAOAction.do?method=displayByICAOs")
                    .data("retrieveLocId", airfield)
                    .data("formatType", "ICAO")
                    .data("reportType", "REPORT")
                    .data("actionType", "notamRetrievalByICAOs")
                    // .userAgent("Mozilla")
                    // .cookie("auth", "token")
                    .timeout(3000).post();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int counter = 0;
        Elements pre = doc.select("pre");

        for (Element link : pre) {

            // get the value from href attribute
            System.out.println("text : " + link.text());
            notamList[counter++] = new Notam(airfield, link.text()
                    .substring(0, 8), link.text().substring(11,
                    link.text().length()));

        }
        counter--;
        while (counter >= 0) {
            System.out.println("class : " + notamList[counter].identifier
                    + "#" + notamList[counter].notamText);
            counter--;
        }
        testText.setText(notamList[0].notamText);
        return null;
    }
}

}


阅读 228

收藏
2020-11-30

共1个答案

小编典典

更改

 protected Void doInBackground(String airfield)

 protected Void doInBackground(String... airfield)

要么

 protected Void doInBackground(String[] airfield)

因为doInBackground()方法需要将字符串数组作为参数

并更改为

   try {
        doc = Jsoup
                .connect(
                        "https://pilotweb.nas.faa.gov/PilotWeb/notamRetrievalByICAOAction.do?method=displayByICAOs")
                .data("retrieveLocId", airfield[0])
                .data("formatType", "ICAO")
                .data("reportType", "REPORT")
                .data("actionType", "notamRetrievalByICAOs")
                // .userAgent("Mozilla")
                // .cookie("auth", "token")
                .timeout(3000).post();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
2020-11-30