Looping AsyncTask Class must either be declared abstract or implement abstract method

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I am trying to loop through some records in my table and for each record, use a value as a parameter to;

  1. get a webpage to parse some html
  2. get some JSON data to parse and get a couple of values from

These both worked perfectly by themselves, but I can not get them both working in an AsyncTask. I have the code here. I know I am probably way off, but if someone could give me a nudge into how my thinking is off, I'd really appreciate it.

On the first line

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

I get

Class FetchWebsiteData must either be declared abstract or implement abstract method doInBackground(params...) in 'AsynTask'  

As an error message and underlined in red.

private class FetchWebsiteData extends AsyncTask<Void, String, Void> {       @Override     protected void onPreExecute() {         super.onPreExecute();         mProgressDialog = new ProgressDialog(summary.this);         mProgressDialog.setMessage("Loading...");         mProgressDialog.setIndeterminate(false);         mProgressDialog.show();     }       protected String doInBackground(String urls3, String result3) {          helper = new TaskDBHelper(summary.this);         SQLiteDatabase sqlDB = helper.getReadableDatabase();          Cursor dataCount = sqlDB.rawQuery("select TASK from " + TaskContract.TABLE, null);         ArrayList<String> temp = new ArrayList<String>();         dataCount.moveToFirst();         do {             temp.add(dataCount.getString(0));         } while (dataCount.moveToNext());         dataCount.close();         StringBuilder sb = new StringBuilder();         for (String s : temp)         {             wallBal = s;              //--------------------------------------------              try {                 Document document = Jsoup.connect(URL+wallBal).get();                 Document doc = Jsoup.parse(document.text());                 balance = new Double(doc.text());                 helper = new TaskDBHelper(summary.this);                 SQLiteDatabase sqlDB2 = helper.getWritableDatabase();                   String sql9 = String.format("UPDATE " + TaskContract.TABLE + " SET " + TaskContract.Columns.OLDBAL + " = " + TaskContract.TABLE + "." + TaskContract.Columns.BAL + " WHERE task='" + wallBal + "'");                 sqlDB2.execSQL(sql9);                  String sql = String.format("UPDATE " + TaskContract.TABLE + " SET " + TaskContract.Columns.BAL + " = " + balance + " WHERE task='" + wallBal + "'");                 sqlDB2.execSQL(sql);                 Cursor dataCount2 = sqlDB.rawQuery("select " + TaskContract.Columns.OLDBAL + " from " + TaskContract.TABLE + " WHERE " + TaskContract.Columns.TASK + " = '" + wallBal + "'", null);                  dataCount2.moveToFirst();                 oldbalance = dataCount2.getDouble(0);                  if(balance != oldbalance) {                     make();                 }                 mProgressDialog.dismiss();                 JSONObject json3 = new JSONObject(result3);                 String str = "";                 JSONArray articles3 = json3.getJSONArray("data");                  str += "articles length = "+json3.getJSONArray("data").length();                 str += "\n--------\n";                 str += "names: "+articles3.getJSONObject(0).getString("MasternodeIP");                 str += "\n--------\n";                   MNIP = articles3.getJSONObject(0).getString("MasternodeIP");                 Sts = articles3.getJSONObject(0).getString("ActiveCount");                  helper = new TaskDBHelper(summary.this);                 SQLiteDatabase sqlDB22 = helper.getWritableDatabase();                  String sql91 = String.format("UPDATE " + TaskContract.TABLE + " SET " + TaskContract.Columns.IP + " = '" + MNIP + "' WHERE task='" + task + "'");                 sqlDB22.execSQL(sql91);                  String sql10 = String.format("UPDATE " + TaskContract.TABLE + " SET " + TaskContract.Columns.STATUS + " = '" + Sts + "' WHERE task='" + task + "'");                 sqlDB22.execSQL(sql10);                   updateUI();               } catch (JSONException | IOException e) {                 e.printStackTrace();             }             //-----------------------------------------------------         }           //--------------------------------------------------------------------                //return null;         return GET(urls3);     }      protected void onPostExecute(Void result) {          updateUI();      } } 

回答1:

Change this

protected String doInBackground(String urls3, String result3) 

To

protected String doInBackground(String... args) 

Then declare your variables like so inside your doInBackground function

String urls3 = args[0]; String result3 = args[1]; 

You will also need to correct your onPostExecute to

protected void onPostExecute(String result) 

And your class declaration to

private class FetchWebsiteData extends AsyncTask<String, Void, String> 


回答2:

The doInBackground method is defined like this:

protected abstract Result doInBackground (Params... params) 

To override it, you need to give it the same parameter list, i.e. a parameter with type Params.... Here, Params, the first type parameter of AsyncTask, is Void.

Furthermore, the result type has to be Result, which in this case is Void, since Result is the third type parameter of AsyncTask, and you're using Void as the third parameter.

So the only way to write a method that overrides doInBackground would be

Void doInBackground(Void... params) 

which I don't think is what you want, since you seem to want the parameters to be strings. So I believe you'll need to change the first type parameter of AsyncTask, which should probably be

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

If you really want doInBackground to return a String, you'll also need to change the third (Result) type parameter:

private class FetchWebsiteData extends AsyncTask<String, String, String> { 

Also, the parameter list has to have the same types; you can't override the method with

protected String doInBackground(String urls3, String result3)  

but instead you need

protected String doInBackground(String... params) 

This appears to be acceptable also:

protected String doInBackground(String[] params) 

If necessary, verify at run time that you got exactly two parameters by checking params.length.



回答3:

You are extending AsyncTask < Void, String, Void > which means you are passing String.. as an argument to doInBackground method and the doInBackground method will return Void.

But, in your code doInBackground method is returning String. So, you should change AsyncTask < Void, String, Void > to AsyncTask < Void, String, String >.

Also, you are missing @Override above the doInBackground method.

Hope it helps! :)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!