Android必须实现继承的抽象方法

时间:2015-06-10 14:53:50

标签: java android inheritance

我已经使用此功能下载了项目,但它运行良好,但是当我将此功能添加到我的项目中时,我收到了错误:

  

类型new AsyncHttpResponseHandler(){}必须实现继承的抽象方法AsyncHttpResponseHandler.onSuccess(int, Header[], byte[])

     

类型onSuccess(String)的方法new AsyncHttpResponseHandler(){}必须覆盖或实现超类型方法

     

类型onFailure(int, Throwable, String)的方法new AsyncHttpResponseHandler(){}必须覆盖或实现超类型方法

我尝试了this question的所有提示,但没有任何帮助。任何可能的解决方案?

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();
    }
}

1 个答案:

答案 0 :(得分:3)

根据你的错误,你不能创建一个新签名来覆盖方法,API你的方法必须具有与超类/接口相同的签名:

Check JLS (§8.4.2)

  

如果[...]具有覆盖等效的签名的方法具有不同的返回类型或不兼容的throws子句,那么这是一个编译时错误。

在您的情况下,此签名必须为:

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) {

恢复.... 声明您的方法,如上所述和AsyncHttpResponseHandler API中所述,并添加它们以满足您的需求