AsyncTask:DoInBackground(String ...)与DoInBackground(Params ...)冲突?

时间:2015-11-09 18:53:00

标签: android string android-asynctask

尝试使用ASyncTask: DoInBackground(String...) clashes with DoInBackground(Params...) in Android.os.AsyncTask; attempting to use incompatible return type 执行HTTP帖子时,我得到以下内容:

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

我该如何解决这个问题?这是我第一次使用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);

        }
    }

来自完整AsyncTask的代码:

function refreshToken(req, res, next){
        res.newToken = "derp";
        next();
}

router.get ('/user/', refreshToken, function(req,res,next){
     res.send("some data");
     console.log(res)
} );

2 个答案:

答案 0 :(得分:8)

声明你这样的课

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

最后一个参数是你将从doInBackground返回的类型,它也输入到onPostExecute。

答案 1 :(得分:2)

来自AsyncTask的文件:

  

异步任务由3种泛型类型定义,称为Params,   进展和结果

您的doInBackground()方法返回String值,但代码中的Result参数为Void。将return的{​​{1}}值从doInBackground()更改为String可以解决问题 - 或者,您可以将Void替换为<String, Void, Void>