3

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();
    }
}
Community
  • 1
  • 1
  • What project did you downloaded? Have you imported the correct classes in your class? – Jorge E. Hernández Jun 10 '15 at 15:00
  • Maybe you are using a different version of https://github.com/loopj/android-async-http where `AsyncHttpResponseHandler` seems to come from. Hence the compiler expects a different method signature – Lars Blumberg Jun 10 '15 at 15:01
  • Yes, I have used wrong version of lib and I though that problem is with some version of java compiler. – Milan Daniel Jun 10 '15 at 15:17

1 Answers1

3

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.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    Oh, thanks you ! I just noticed that I used version 1.4.6 of android-async-http and in the downloaded project was version 1.4.4. Thast why one project worked and another not... – Milan Daniel Jun 10 '15 at 15:07