有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法重写JsonHttpResponseHandler中的onSuccess()方法

我正在http://www.raywenderlich.com/78578/安卓-tutorial-for-beginners-part-3学习restful服务

当遇到

 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 安卓", 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 安卓", 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.安卓:安卓-async-http:1.4.9'
    compile 'com.squareup.picasso:picasso:2.5.2'
}

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


共 (3) 个答案

  1. # 1 楼答案

    onSuccess函数调用的开始处

    super.onSuccess();
    

    这将修复您的错误。重写方法时,通常也会调用原始方法(超类的方法)

    同样适用于onFailure

    super.onFailure();
    

    您可以检查此link以查看每个回调函数的参数

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

  2. # 2 楼答案

    只要使用正确的标题

    尝试:

    @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) {
    
    }
    
  3. # 3 楼答案

    检查你的进口

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

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