我在尝试使用Okhttp进行改造时遇到了问题。我似乎不明白我做错了什么。
它显示错误:'Anonymous class derived from Callback' must either be declared abstract or implement abstract method 'onResponse(Response<T>, Retrofit)' in 'Callback'
在我的MainActivity中我有这个:
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("User-Agent", "Your-App-Name")
.header("Content-Type", "application/json")
.header("Authorization","authorization_code")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build();
TestInterface service = retrofit.create(TestInterface.class);
Call<TestData> call = service.getPost();
/* It keeps pointing at this line below:
'Callback' must either be declared abstract" */
call.enqueue(new Callback<TestData>() {
@Override
public void onResponse(Response<TestData> response, Retrofit retrofit) {
// Get result Repo from response.body()
// response.isSuccess() is true if the response code is 2xx
int statusCode = response.code();
if (response.isSuccess()) {
System.out.println("Success: ");
} else {
}
}
@Override
public void onFailure(Throwable t) {
System.out.println("failed: "+t);
}
});
在我的TestData界面中,我有这个
public interface TestInterface {
@POST("/paths_to_web_directory")
Call<TestData>
getPost();
}
这是我在其他示例中看到的方式,所以也许我以错误的方式实现它。请纠正我。感谢
答案 0 :(得分:0)
改装github项目有一个具有不同方法签名的样本。试试这个:
call.enqueue(new Callback<TestData>() {
@Override public void onResponse(Call<TestData> call, Response<TestData> response) {
}
@Override public void onFailure(Call<TestData> call, Throwable t) {
}
});