Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask: DoInBackground(String...) clashes with DoInBackground(Params...)?

When trying to use Async task to carry out an HTTP post I get the following:

ASyncTask: DoInBackground(String...) clashes with DoInBackground(Params...) in Android.os.AsyncTask; attempting to use incompatible return type

How do I fix this? This is my first time using AsyncTask.

Specific line causing error:

@Override
        protected String doInBackground(String... params) {

Code from the full AsyncTask:

private class MyTask extends AsyncTask<String, Void, Void>
    {
        boolean success = false;

        @Override
        protected String doInBackground(String... params) {
            StringBuilder respData = new StringBuilder();
            URL url = new URL("MY_URL");
            URLConnection conn = url.openConnection();
            HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;

            httpUrlConnection.setUseCaches(false);
            httpUrlConnection.setRequestProperty("User-Agent", "App");
            httpUrlConnection.setConnectTimeout(30000);
            httpUrlConnection.setReadTimeout(30000);

            httpUrlConnection.setRequestMethod("POST");
            httpUrlConnection.setDoOutput(true);

            OutputStream os = httpUrlConnection.getOutputStream();
            //InputStream postStream = toInputStream(toSubmit, "UTF-8");
            InputStream stream = new ByteArrayInputStream(toSubmit.getBytes(StandardCharsets.UTF_8));
            try {
                copy(stream, os);
            } finally {
                stream.close();
                os.flush();
                os.close();
            }

            httpUrlConnection.connect();

            int responseCode = httpUrlConnection.getResponseCode();

            if (200 == responseCode) {
                InputStream is = httpUrlConnection.getInputStream();
                InputStreamReader isr = null;
                try {
                    isr = new InputStreamReader(is);
                    char[] buffer = new char[1024];
                    int len;
                    while ((len = isr.read(buffer)) != -1) {
                        respData.append(buffer, 0, len);
                    }
                } finally {
                    if (isr != null)
                    {
                        isr.close();
                        success = true;
                    }
                }
                is.close();
            }
            else {
                // use below to get error stream
                // inputStream = httpUrlConnection.getErrorStream();
            }
            return "done";
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);

        }
    }
like image 928
java12340000 Avatar asked Nov 09 '15 18:11

java12340000


People also ask

What are the problems in AsyncTask?

In summary, the three most common issues with AsyncTask are: Memory leaks. Cancellation of background work. Computational cost.

What is an AsyncTask deprecated in API level 30?

This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

What is AsyncTask in Android with example?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.

What method must be overridden for using AsyncTask?

AsyncTask must be subclassed to be used. The subclass will override at least one method ( doInBackground(Params...) ), and most often will override a second one ( onPostExecute(Result) .)


1 Answers

Declare you class like this

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

The last param is type you will return from doInBackground and its also input to onPostExecute.

like image 93
prashant Avatar answered Oct 15 '22 15:10

prashant