2016-01-20 78 views
2

我有一个问题,试图使用Okhttp加装。我似乎不明白我做错了什么。使用Retrofit:2.0与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

你正在使用哪种改进版本?你可以尝试从onResponse中删除第二个参数吗? – davehenry

+0

我正在使用改造:2.0.0-beta2 –

+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) { 
      } 
}); 
+2

谢谢,但这没有奏效 –

+0

你看到什么异常或错误? – davehenry

+0

它根本没有编译,说不能解决,我想我只需要回退到使用Okhttp和拦截器的改造1 –