我正在 http://www.raywenderlich.com/78578/android-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 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 sync 任务成功了。但我无法弄清楚为什么 onSuccess 方法被启发为(方法不覆盖父类(super class))
甚至我已经将 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
甚至我也尝试过使用接口(interface)。我正在使用异步 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() 方法没有覆盖父类(super class)
最佳答案
检查你的进口
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 并覆盖了不存在的方法。我认为这些可能在旧版本中可用。
关于java - 无法覆盖 JsonHttpResponseHandler 中的 onSuccess() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33646583/