Search code examples
androidandroid-asynctask

CURLTask abstract method doInBackground(String...) override in AsyncTaskextension does not override?


I am throwing to get something from but the is my asynctask declaration is withing MainActivity.

class CURLTask extends AsyncTask<String,Void,String> {

    protected String doInBackground(String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            bufferedReader.close();
            urlConnection.disconnect();
            return stringBuilder.toString();
        }
        catch(Exception e) {

            return "";
        }
        
    }



    protected void onPostExecute(String recvString ) {
        String stnlist =  recvString;
    }
}

Creating an instance of CURLtask and calling execute from onCreate

new CURLTask().execute("someApiUrl.com");

Compile Error:

Error:(60, 1) error: MainActivity.CURLTask is not abstract and does not override abstract method doInBackground(String...) in AsyncTask

Also: Hovering mouse over **CURLtask extend AsyncTask**, Android Studio says >Class CURLtask must either be declared abstract or implement abstract method doInBackground

And also hovering pointer over the doInBackground declaration says:

Method doInBackground is never used


Solution

  • The doInBackground method is declared and used slightly incorrectly. Should be:

    @Override
    protected String doInBackground(String... parms) {
        String urlString = parms[0];
        ...
    }
    

    Of course, my snippet has no error handling in it, so be sure to check your input parameters accordingly.