【问题标题】:Can't override onSuccess() method in JsonHttpResponseHandler无法覆盖 JsonHttpResponseHandler 中的 onSuccess() 方法
【发布时间】:2015-11-11 08:18:45
【问题描述】:

我在http://www.raywenderlich.com/78578/android-tutorial-for-beginners-part-3学习宁静服务

偶遇时

 AsyncHttpClient client = new AsyncHttpClient();
        client.get(QUERY_URL + urlString,
        new JsonHttpResponseHandler() {

            @Override
public void onSuccess(JSONObject jsonObject) {
// Display a "Toast" message
// to announce your success
Toast.makeText(getApplicationContext(), "Success!",   Toast.LENGTH_LONG).show();

// 8. For now, just log results
Log.d("omg android", jsonObject.toString());
}      
@Override

public void onFailure(int statusCode, Throwable throwable, JSONObject  error) {
// Display a "Toast" message 
// to announce the failure
Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " +    throwable.getMessage(), Toast.LENGTH_LONG).show();

// Log error message
// to help solve any problems
Log.e("omg android", statusCode + " " + throwable.getMessage());
}
        });

我的 gradle 同步任务成功了。但是我不明白为什么 onSuccess 方法被启发为(方法不覆盖超类)

即使我已经将 onSuccess() 参数更改为

 public void onSuccess(int statusCode, org.apache.http.Header[] headers, JSONObject jsonObject)

我也遵循了以下链接中提供的所有解决方案

method does not override or implement a method from a supertype - for Override

AndroidAsyncHttp Error

http://answered.site/-heres-my-mainactivityjava-the-part-of-the-code-it-occurs-in-the-querybooks/2950406/

即使我也尝试过使用接口。而且我使用的是异步 http 1.4.9 所以我把gradle脚本改成了

dependencies {

    ...

    // there may or may not be a support library above these
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.squareup.picasso:picasso:2.5.2'
}

我已经尝试了 stackoverflow 和 github 中可用的所有解决方案。但我仍然无法清除该错误,它显示 onSuccess() 和 onFailure() 方法不会覆盖超类

【问题讨论】:

    标签: java android rest asynchttpclient


    【解决方案1】:

    检查你的进口

    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.JsonHttpResponseHandler;
    
    import org.json.JSONObject;
    
    **import cz.msebera.android.httpclient.Header;**
    

    试试这个

    client.get("sdsd", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
    
        }
    
        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
        }
    });
    

    您使用了错误的 Header 并覆盖了不存在的方法。我想可能是那些在旧版本中可用。

    【讨论】:

      【解决方案2】:

      onSuccess 函数调用开始时

      super.onSuccess();
      

      这将解决您的错误。当你重写一个方法时,通常也会调用原始方法(超类的方法)。

      onFailure 也一样

      super.onFailure();
      

      你可以查看link查看每个回调函数的参数。

      正如 @hegazy 已经提到的,您对函数使用了错误的标题,您遵循的教程很可能已经过时了。检查链接以获取具有正确标题的所有可能功能。在super 调用中,您可以传递相同的变量。

      【讨论】:

      • 超级调用必须放在开头,而不是结尾
      • @fattidare 供将来参考,但并非总是如此。例如。 super.onDestroy() 通常在最后调用
      • @TimCastelijns 你的做法不正确。有时你需要把它放在最后,但这不是最好的方法
      【解决方案3】:

      只需使用正确的标题

      试试:

      @Override
      public void  onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
         // called when response HTTP status is "200 OK"
      }
      
      @Override
      public void onFailure(int statusCode,cz.msebera.android.httpclient.Header[] headers, Throwable e, JSONObject response) {
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-05
        相关资源
        最近更新 更多