【问题标题】:I am getting the error AsyncTask doesn't override methods from superclass?我收到错误 AsyncTask 不会覆盖超类中的方法?
【发布时间】:2016-12-27 07:58:35
【问题描述】:

它表示 Async 不会覆盖超类中的任何方法。 这是取自 tutorialspoint 网站的示例。 这似乎不起作用。请帮忙。 提前致谢。 *编辑:我已经上传了完整的 java 文件。

public class SigninActivity extends AsyncTask{

        private Context context;
        private TextView statusField,userField;

        public SigninActivity(Context context,TextView statusField,TextView userField) {
            this.context = context;
            this.statusField = statusField;
            this.userField = userField;
    }

        protected void onPreExecute(){
        }


        @Override
        protected String doInBackground(String... arg0) {
            try{
                String username = (String)arg0[0];
                String password = (String)arg0[1];

                String link="http://127.0.0.1/login.php";
                String data  = URLEncoder.encode("username", "UTF-8") + "=" +
                        URLEncoder.encode(username, "UTF-8");
                data += "&" + URLEncoder.encode("password", "UTF-8") + "=" +
                        URLEncoder.encode(password, "UTF-8");

                URL url = new URL(link);
                URLConnection conn = url.openConnection();

                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                wr.write( data );
                wr.flush();

                BufferedReader reader = new BufferedReader(new
                        InputStreamReader(conn.getInputStream()));

                StringBuilder sb = new StringBuilder();
                String line = null;

                // Read Server Response
                while((line = reader.readLine()) != null) {
                    sb.append(line);
                    break;
                }

                return (sb.toString());
            }
            catch(Exception e){
               return (new String("Exception: " + e.getMessage()));
            }
        }
    @Override
        protected void onPostExecute(String result){
            this.statusField.setText("Login Successful");
            this.userField.setText(result);
        }
    }

【问题讨论】:

  • 你要扩展哪个类
  • 我猜你没有为AsyncTask 使用正确的类型参数——但我们看不到,因为你只提供了方法,而不是minimal reproducible example
  • 我正在扩展 AsyncTask@Mr.Popular
  • 发布您的完整异步任务类代码..
  • @NitinMamidala。我们还需要类型。你必须有类似AsyncTask<String, Void, String> 的东西。该部分在您的代码中是什么样的?

标签: java android asynchronous android-asynctask overriding


【解决方案1】:
 public class SigninActivity extends AsyncTask<String, String, String>

使用上面的代码就可以了

【讨论】:

  • 如果你需要 preExecute 和 postExcute 你必须扩展上面的代码,或者如果你只需要覆盖 doinBackground 你可以使用 public class Sample extends AsyncTask{ @Override protected Object doInBackground(Object[] objects) { 返回空值; } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
相关资源
最近更新 更多