无法覆盖 JsonHttpResponseHandler 中的 onSuccess() 方法
Can't override onSuccess() method in JsonHttpResponseHandler
我正在 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 同步任务是 successful.But 我不明白为什么 onSuccess 方法被启发为(方法不覆盖超级 class)
甚至我已经将 onSuccess() 参数更改为
public void onSuccess(int statusCode, org.apache.http.Header[] headers, JSONObject jsonObject)
而且我已经遵循了以下链接中提供的所有解决方案
我什至尝试过使用接口 also.And 我正在使用异步 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'
}
我已经尝试了 Whosebug 中可用的所有解决方案,但 github.But 仍然无法清除该错误,它显示 onSuccess() 和 onFailure() 方法没有覆盖 superclass
在 onSuccess
函数调用开始时
super.onSuccess();
这将解决您的错误。当您重写一个方法时,通常还会调用原始方法(超级 class 的方法)。
onFailure
也一样
super.onFailure();
你可以查看这个link来查看每个回调函数的参数。
正如 @hegazy 已经提到的,您对函数使用了错误的 headers,您遵循的教程很可能已经过时了。使用正确的 headers 检查 link 所有可能的功能。在 super
调用中,您可以传递相同的变量。
检查你的进口
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 并重写了不存在的方法。我想可能是那些在旧版本中可用。
只要用对header
尝试:
@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) {
}
我正在 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 同步任务是 successful.But 我不明白为什么 onSuccess 方法被启发为(方法不覆盖超级 class)
甚至我已经将 onSuccess() 参数更改为
public void onSuccess(int statusCode, org.apache.http.Header[] headers, JSONObject jsonObject)
而且我已经遵循了以下链接中提供的所有解决方案
我什至尝试过使用接口 also.And 我正在使用异步 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'
}
我已经尝试了 Whosebug 中可用的所有解决方案,但 github.But 仍然无法清除该错误,它显示 onSuccess() 和 onFailure() 方法没有覆盖 superclass
在 onSuccess
函数调用开始时
super.onSuccess();
这将解决您的错误。当您重写一个方法时,通常还会调用原始方法(超级 class 的方法)。
onFailure
super.onFailure();
你可以查看这个link来查看每个回调函数的参数。
正如 @hegazy 已经提到的,您对函数使用了错误的 headers,您遵循的教程很可能已经过时了。使用正确的 headers 检查 link 所有可能的功能。在 super
调用中,您可以传递相同的变量。
检查你的进口
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 并重写了不存在的方法。我想可能是那些在旧版本中可用。
只要用对header
尝试:
@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) {
}