【问题标题】:AsyncTask: DoInBackground(String...) clashes with DoInBackground(Params...)?AsyncTask:DoInBackground(String...) 与 DoInBackground(Params...) 冲突?
【发布时间】:2015-11-09 18:52:59
【问题描述】:

当尝试使用 Async task 执行 HTTP 发布时,我得到以下信息:

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

我该如何解决这个问题?这是我第一次使用 AsyncTask。

导致错误的特定行:

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

        }
    }

【问题讨论】:

    标签: android string android-asynctask


    【解决方案1】:

    像这样声明你的课

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

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

    【讨论】:

    • 谢谢,解决了,但我知道 postexecute 出现错误:onPostExecute(java.lang.string) 不能应用于 (java.lang.void) ?
    • 将你的 onPostExecute 更改为 protected void onPostExecute(String result)。
    【解决方案2】:

    来自AsyncTask 的文档:

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

    您的doInBackground() 方法返回一个String 值,但您的代码中的Result 参数是Void。将doInBackground()return 值从String 更改为Void 将解决问题 - 或者,您可以将&lt;String, Void, Void&gt; 替换为&lt;String, Void, String&gt;

    【讨论】:

    • 谢谢,解决了,但我知道 postexecute 出现错误:onPostExecute(java.lang.string) 不能应用于 (java.lang.void) ?
    • @java12340000 将protected String doInBackground(String... params) 替换为protected Void doInBackground(String... params),从doInBackground() 返回null 而不是"done",并将您的类声明保留为private class MyTask extends AsyncTask&lt;String, Void, Void&gt; - 似乎是您情况下的最佳解决方案
    猜你喜欢
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    相关资源
    最近更新 更多