类型<>必须实现继承的抽象方法AsyncTask.doInBackground(Object ...)

时间:2014-01-05 10:08:22

标签: android mysql inheritance android-asynctask

我目前正在开发一个连接到MySQL数据库的Android应用程序。现在我的AsyncTask出了问题。它给了我错误:

  

类型MainActivity.NetCheck必须实现继承的抽象方法AsyncTask.doInBackground(Object...)

     

类型MainActivity.ProcessLogin必须实现继承的抽象方法AsyncTask.doInBackground(Object...)

以下是代码:

/**
 * Async Task to check whether internet connection is working.
 **/
    private class NetCheck extends AsyncTask
    {
        private ProgressDialog nDialog;
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            nDialog = new ProgressDialog(MainActivity.this);
            nDialog.setTitle("Checking Network");
            nDialog.setMessage("Loading..");
            nDialog.setIndeterminate(false);
            nDialog.setCancelable(true);
            nDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... args){
/**
 * Gets current device state and checks for working internet connection by trying Google.
**/
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()) {
                try {
                    URL url = new URL("http://www.google.com");
                    HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                    urlc.setConnectTimeout(3000);
                    urlc.connect();
                    if (urlc.getResponseCode() == 200) {
                        return true;
                    }
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return false;
        }
        @Override   
        protected void onPostExecute(Boolean th){
            if(th == true){
                nDialog.dismiss();
                new ProcessLogin().execute();
            }
            else{
                nDialog.dismiss();
                loginErrorMsg.setText("Error in Network Connection");
            }
        }
    }

/**
     * Async Task to get and send data to My Sql database through JSON response.
     **/
    private class ProcessLogin extends AsyncTask {
        private ProgressDialog pDialog;
        String UserID,Password;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loginUName = (EditText) findViewById(R.id.loginUsername);
            loginPword = (EditText) findViewById(R.id.loginPassword);
            UserID = loginUName.getText().toString();
            Password = loginPword.getText().toString();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setTitle("Contacting Servers");
            pDialog.setMessage("Logging in ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected JSONObject doInBackground(String... args) {
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.loginUser(UserID, Password);
            return json;
        }
        @Override
        protected void onPostExecute(JSONObject json) {
            try {
               if (json.getString(KEY_SUCCESS) != null) {
                    String res = json.getString(KEY_SUCCESS);
                    if(Integer.parseInt(res) == 1){
                        pDialog.setMessage("Loading User Space");
                        pDialog.setTitle("Getting Data");
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");
                        /**
                         * Clear all previous data in SQlite database.
                         **/
                        UserFunctions logout = new UserFunctions();
                        logout.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_FIRSTNAME),json_user.getString(KEY_LASTNAME),json_user.getString(KEY_EMAIL),json_user.getString(KEY_USERNAME),json_user.getString(KEY_UID),json_user.getString(KEY_CREATED_AT));
                       /**
                        *If JSON array details are stored in SQlite it launches the User Panel.
                        **/
                        Intent upanel = new Intent(getApplicationContext(), Index.class);
                        upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        pDialog.dismiss();
                        startActivity(upanel);
                        /**
                         * Close Login Screen
                         **/
                        finish();
                    }else{
                        pDialog.dismiss();
                        loginErrorMsg.setText("Incorrect username/password");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
       }
    }
    public void NetAsync(View view){
        new NetCheck().execute();
    }   

3 个答案:

答案 0 :(得分:4)

AsyncTask需要定义3种泛型类型:

AsyncTask<Params, Progress, Result>

在你的情况下它应该是

AsyncTask<String, Void, Boolean>

所以只需像这样延伸:

private class NetCheck extends AsyncTask<String, Void, Boolean>

答案 1 :(得分:2)

您的AsyncTask缺少通用类型参数,因此假设Object,但您的实现使用更具体的类作为类型。

所以,改变这个

private class NetCheck extends AsyncTask

private class NetCheck extends AsyncTask<String, Void, Boolean>

第一个泛型类型参数是doInBackground的参数类型。第二个是进度更新的参数类型,第三个是结果类型。

您也需要对ProcessLogin课程进行类似的更改。

答案 2 :(得分:0)

更改您的下载类声明
private class NetCheck extends AsyncTask{
}

就像

private class NetCheck extends AsyncTask<String,Void,String>{
}

和另一个AsyncTask

的情况相同
private class ProcessLogin extends AsyncTask{
}

就像

private class ProcessLogin extends AsyncTask<String,Void,String>{
}

因为AsyncTask需要定义3个泛型类型,如下所示:

AsyncTask<Params, Progress, Result>