首页 文章

为什么AsyncTask无法正常工作?

提问于
浏览
-4
String json_string;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void getJSON (View view){
    new BackgroundTask().execute();
}

class BackgroundTask extends AsyncTask <Void, Void, String>{
    String json_url;
    @Override
    protected void onPreExecute() {
        json_url = "http://androidtut.comli.com/json_get_data.php";
    }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();

            while ((json_string = bufferedReader.readLine()) != null) {

                stringBuilder.append(json_string + "\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();


        }
        catch (MalformedURLException e){
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        TextView textView = (TextView) findViewById(R.id.textJSON);
        textView.setText(result);
    }
}

错误:(31,5)错误:MainActivity.BackgroundTask不是抽象的,并且不会覆盖AsyncTask中的抽象方法doInBackground(Void ...)

错误:(39,24)错误:MainActivity.BackgroundTask中的doInBackground(Void ...)无法覆盖AsyncTask中的doInBackground(Params ...)返回类型Void与String不兼容,其中Params,Result是类型变量:Params extends在类AsyncTask中声明的对象结果扩展了在类AsyncTask中声明的Object

错误:(38,9)错误:方法不会覆盖或实现超类型的方法

错误:(54,53)错误:不兼容的类型:字符串无法转换为Void

错误:任务':app:compileDebugJavaWithJavac'执行失败 . >编译失败;请参阅编译器错误输出以获取详细信

5 回答

  • 2

    如果你无法清楚地理解AsyncTask中的功能,你最好尝试在AndroidStudio中生成该功能 . 这是AndroidStudio生成的代码 .

    class BackgroundTask extends AsyncTask<Void,Void,String>
    {
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    
        @Override
        protected String doInBackground(Void... params) {
            return null;
        }
    }
    

    比较你的代码与上面,我想你会得到答案 .

  • 0

    AsyncTask <Void, Void, String>

    这里的最后一个类型是返回结果类型 .

    您的 doInBackground 目前使用 Void 定义,而不是 String

    同样,您不能将 StringBuilder String结果作为 Void 类型返回 .


    您可能会发现处理JSON的the other Android HTTP libraries更容易使用 .

  • 0

    AFAIK,当您在AsyncTask中定义Void时,您应该理解其含义 .

    • doInBackground是一个带空参数的函数(第一个void),返回一个String(最后一个String)

    protected String doInBackground ()

    • onProgressUpdate是一个带有空参数的函数(第二个空格)

    protected void onProgressUpdate()

    • onPostExecute是一个带String参数的函数(来自步骤1的值) .

    protected void onPostExecute(String)

  • 0
    String json_string;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void getJSON (View view){
        new AsynsTask(){
            String json_url;
            @Override
            protected void onPreExecute() {
                json_url = "http://androidtut.comli.com/json_get_data.php";
            }
    
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    URL url = new URL(json_url);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder stringBuilder = new StringBuilder();
    
                    while ((json_string = bufferedReader.readLine()) != null) {
    
                        stringBuilder.append(json_string + "\n");
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return stringBuilder.toString().trim();
    
    
                }
                catch (MalformedURLException e){
                    e.printStackTrace();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
    
            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
            }
    
            @Override
            protected void onPostExecute(String result) {
                TextView textView = (TextView) findViewById(R.id.textJSON);
                textView.setText(result);
            }
        }.execute();
    
     }
    
  • -1

    我在这里发现了两个错误 .
    1.对于任务执行,

    new BackgroundTask().execute(null);
    
    1. doInBackground返回类型应与Result类型匹配(即String)
    protected String doInBackground(Void... voids)
    

    AsyncTask

    String json_string;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void getJSON (View view){
            new BackgroundTask().execute(null);
        }
    
        class BackgroundTask extends AsyncTask <Void, Void, String>{
            String json_url;
            @Override
            protected void onPreExecute() {
                json_url = "http://androidtut.comli.com/json_get_data.php";
            }
    
            @Override
            protected String doInBackground(Void... voids) {
                try {
                    URL url = new URL(json_url);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder stringBuilder = new StringBuilder();
    
                    while ((json_string = bufferedReader.readLine()) != null) {
    
                        stringBuilder.append(json_string + "\n");
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return stringBuilder.toString().trim();
    
    
                }
                catch (MalformedURLException e){
                    e.printStackTrace();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
    
            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
            }
    
            @Override
            protected void onPostExecute(String result) {
                TextView textView = (TextView) findViewById(R.id.textJSON);
                textView.setText(result);
            }
        }
    

相关问题