1

I am writing an Android client for consuming a SOAP based web service. I am using an AsyncTask for performing the networking (that is calling the webservice) operation. I am in the middle of writing the client.

  1. Now as I try to implement the AsyncTask's doInBackground method, I get the following two errors at protected String doInBackground(String[] params)... if I write the @Override annotation with it:

    The method doInBackground(String[]) of type MainActivity.MyTask must override or implement a supertype method. 1 Quick fix: Remove '@Override' annotation

    and the following error at private class MyTask extends AsyncTask... :

    The type MainActivity.MyTask must implement the inherited abstract method AsyncTask.doInBackground(String[]...). 2 Quick fixes: Add unimplemented methods (this auto-generates the doInBackground method again with the @Override annotation). Make type 'MyTask' abstract.

  2. if I don't write the @Override annotation, I get only the second error.

    ...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            String [] params = {URL, METHOD_NAME, NAMESPACE}; 
        }
    
        private class MyTask extends AsyncTask<String[], Void, String> {
    
            //@Override write it or not write it? :s ****************
            protected String doInBackground(String[] params) {
                // TODO Auto-generated method stub
                return null;
            }
        }
    ...
    

What might be the reason behind this? What should I do?

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    Change `private class MyTask extends AsyncTask` to `private class MyTask extends AsyncTask` and write `String...` instead of `String[]` in `doInBackground argments` – MysticMagicϡ Nov 15 '14 at 12:20

3 Answers3

7

You have syntax error:

Change

private class MyTask extends AsyncTask<String[], Void, String> 

to

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

and write String... instead of String[] in doInBackground arguments.

protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    return null;
}

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
2

Since the method in AsyncTask<String[], Void, String> is declared as doInBackground(String[]... params) - possibly after substituting in the right type parameter, you need to have the same parameter types when you overrride it in MyTask. It looks like you've written doInBackground(String[] params) instead of doInBackground(String[]... params).

Note that that would mean that params would be a whole sequence of arrays of Strings. That may not be what you want. Did you really mean for MyTask to extend AsyncTask<String[], Void, String>?

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Oh my bad, it should rather have been a varargs `doInBackground(String... params)`, I believe because the `AsyncTask` class itself which is the super type must be having a vararg argument in its `doInBackground`. Doesn't matter in my program even though I am passing a String array, because varargs are treated just like arrays I believe. Thank you, this solves my problem. – Solace Nov 15 '14 at 12:30
  • 1
    If you want to pass `String... params` here, then you probably want to change `MyTask` so that it extends `AsyncTask` as given in MysticMagic's answer. – Dawood ibn Kareem Nov 15 '14 at 12:32
1

The doInBackground method should be written as

doInBackground(Params... params)
javawocky
  • 899
  • 2
  • 9
  • 31
  • How will that help, when the compile error states that the class has to implement `AsyncTask.doInBackground(String[]...)`? – Dawood ibn Kareem Nov 15 '14 at 12:26
  • Params being meant as a general placeholder here, not that he should write that explicitly instead of String[]... I should have been clearer – javawocky Nov 15 '14 at 12:29