6

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);

        }
    }
java12340000
  • 101
  • 1
  • 5

2 Answers2

9

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.

prashant
  • 3,570
  • 7
  • 40
  • 50
3

From the documenation for AsyncTask:

An asynchronous task is defined by 3 generic types, called Params, Progress and Result

Your doInBackground() method returns a String value, but the Result parameter is Void in your code. Changing the return value of doInBackground() from String to Void will correct the problem - or, you can substitute <String, Void, Void> for <String, Void, String>

PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • Thanks, that solved it but I know get the error with postexecute: onPostExecute(java.lang.string) cannot be applied to (java.lang.void) ? – java12340000 Nov 09 '15 at 19:06
  • @java12340000 Substitute `protected String doInBackground(String... params)` for `protected Void doInBackground(String... params)`, return `null` rather than `"done"` from `doInBackground()` and keep your class declaration as `private class MyTask extends AsyncTask` - Seems the best solution in your case – PPartisan Nov 09 '15 at 19:08