android - 改造httpLogInterceptor java.lang.NoSuchMethodError :

标签 android retrofit okhttp

我正在通过 HttpLogInterceptor 实现打印日志。但是发生了一个错误。

10-04 05:22:55.753 21294-21851/com.tc.retorfit_okhttp_cache_technique E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.tc.retorfit_okhttp_cache_technique, PID: 21294
java.lang.NoSuchMethodError: No virtual method log(Ljava/lang/String;)V in class Lokhttp3/internal/Platform; or its super classes (declaration of 'okhttp3.internal.Platform' appears in /data/data/com.tc.retorfit_okhttp_cache_technique/files/instant-run/dex/slice-okhttp-3.3.0_b69c4d16ac6ddca06e0d47d36f15c48706942fa9-classes.dex)
at okhttp3.logging.HttpLoggingInterceptor$Logger$1.log(HttpLoggingInterceptor.java:109)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:157)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.access$100(RealCall.java:33)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at okhttp3.logging.HttpLoggingInterceptor$Logger$1.log(HttpLoggingInterceptor.java:109)

错误在下面的代码中:

    public interface Logger {
    void log(String message);

    /** A {@link Logger} defaults output appropriate for the current platform. */
    Logger DEFAULT = new Logger() {
      @Override public void log(String message) {
        Platform.get().log(message);//Here have a error,NoSuchMethodError
      }
    };
  }

我的代码是下面的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_print_log);
    HttpLoggingInterceptor httpLoggingInterceptor=new HttpLoggingInterceptor();//compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'
    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okHttpClient=new OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .build();

    Retrofit retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl("https://api.github.com/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    final PrintLogActivity.GitHubApi repo=retrofit.create(PrintLogActivity.GitHubApi.class);
            Call<List<Contributor>> responseBodyCall = repo.getContributors("square","retrofit");
            responseBodyCall.enqueue(new Callback<List<Contributor>>() {
                @Override
                public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
                    Gson gson=new Gson();
                    if (response.body()!=null&&response.body().size()>0){
                        for (Contributor contributor:response.body()){

                            Log.i("contributor",contributor.getLogin());
                        }
                    }else{
                        Log.i("contributor","list is null");
                    }
                }

                @Override
                public void onFailure(Call<List<Contributor>> call, Throwable t) {
                    Log.e("contributor","reuquire fail");
                }
            });
            responseBodyCall.cancel();

}
interface GitHubApi{

    @GET("repos/{owner}/{repo}/contributors")
    Call<ResponseBody> contributorsBySimpleGetCall(@Path("owner") String own, @Path("repo") String repo);

    @GET("users/{user}")
    Call<ResponseBody> getUser(@Path("user") String user);

    @GET("repos/{owner}/{repo}/contributors")
    Call<List<Contributor>> getContributors(@Path("owner") String own,@Path("repo") String repo);
}

最佳答案

确保你的 okHttplogging-interceptor 在 build.gradle 中有相同的版本

喜欢

com.squareup.okhttp3:okhttp:3.4.1

com.squareup.okhttp3:logging-interceptor:3.4.1

这里的版本是相同的3.4.1

关于android - 改造httpLogInterceptor java.lang.NoSuchMethodError :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39844870/

相关文章:

android - 我们应该将 google 的安全提供程序与 OkHttp 一起使用吗?

android - 使用 retrofit2+okhttp3 缓存响应数据

android - 如何在应用程序关闭时重新启动服务?

Android Phonegap 应用程序 - 将 Activity 放在首位

java - For Loop 遍历数组中的 JSON 对象

java - 从 recyclerview 中的 editText 进行搜索,我使用改造从 API 获取项目

android - Retrofit 2 不解析响应以防出错

android - 即使连接超时和读取超时设置为默认值(无限),接收请求超时?

android - holder.getSurface().isValid() 返回 false

android - 使用 Retrofit 2 记录响应