Milan Daniel
Milan Daniel

Reputation: 77

Android must implement the inherited abstract method

I have downloaded project with this function where it works well, but when I coppied this function into my project, I get errors :

The type new AsyncHttpResponseHandler(){} must implement the inherited abstract method AsyncHttpResponseHandler.onSuccess(int, Header[], byte[])

The method onSuccess(String) of type new AsyncHttpResponseHandler(){} must override or implement a supertype method

The method onFailure(int, Throwable, String) of type new AsyncHttpResponseHandler(){} must override or implement a supertype method

I tried all tips from this question but nothing helped. Any possible solution ?

public void syncSQLiteMySQLDB(){
    //Create AsycHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    ArrayList<HashMap<String, String>> userList =  controller.getAllUsers();
    if(userList.size()!=0){
        if(controller.dbSyncCount() != 0){
            prgDialog.show();
            params.put("usersJSON", controller.composeJSONfromSQLite());
            client.post("http://techkeg.tk/sqlitemysqlsync/insertuser.php",params ,new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    System.out.println(response);
                    prgDialog.hide();
                    try {
                        JSONArray arr = new JSONArray(response);
                        System.out.println(arr.length());
                        for(int i=0; i<arr.length();i++){
                            JSONObject obj = (JSONObject)arr.get(i);
                            System.out.println(obj.get("id"));
                            System.out.println(obj.get("status"));
                            controller.updateSyncStatus(obj.get("id").toString(),obj.get("status").toString());
                        }
                        Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Throwable error, String content) {
                    prgDialog.hide();
                    if(statusCode == 404){
                        Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                    }else if(statusCode == 500){
                        Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }else{
            Toast.makeText(getApplicationContext(), "SQLite and Remote MySQL DBs are in Sync!", Toast.LENGTH_LONG).show();
        }
    }else{
            Toast.makeText(getApplicationContext(), "No data in SQLite DB, please do enter User name to perform Sync action", Toast.LENGTH_LONG).show();
    }
}

Upvotes: 3

Views: 2384

Answers (1)

Jordi Castilla
Jordi Castilla

Reputation: 26991

You cannot make a new signature to overriden methods, according your error and API your methods MUST have same signature than super class/interface:

Check JLS (§8.4.2)

It follows that is a compile-time error if [...] a method with a signature that is override-equivalent [...] has a different return type or incompatible throws clause.

In your case this signatures MUST be:

public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
     // Successfully got a response
 }

public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
{
     // Response failed :(
}

NOT:

public void onSuccess(String response) {
public void onFailure(int statusCode, Throwable error, String content) {

Resuming.... declare your methods like described above and in AsyncHttpResponseHandler API and addapt them to achieve your needs.

Upvotes: 3

Related Questions