retrofit - java.lang.NoClassDefFoundError : Failed resolution of: Lokio/Buffer;

标签 retrofit

当我使用retrofit时,我得到了异常java.lang.NoClassDefFoundError:无法解析:Lokio/Buffer;我使用okhttpclient来设置retrofit的 header 。get userList是一个post方法,我需要发送正文应要求。

private void getUserList(int startIndex){
    final JSONObject audienceObj = ProtocolHelper.getProtocolUtils(mContext).getUserlistJsonObj(mRoomData.mUid, mRoomData.mRoomId, startIndex);
    OkHttpClient okClient = new OkHttpClient.Builder()
    .addInterceptor(
        new Interceptor() {
          @Override
          public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();

                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder()
                        .header("sessionId", CommonData.getUserInfo(mContext).sessionId);
                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        })
    .build();


    String baseUrl = ProtocolUtils.BASE_URL+"/";
    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(GsonConverterFactory.create())
    .client(okClient)
    .build();

    String audienceUrl = ProtocolHelper.getProtocolUtils(mContext).getProtocolUrl(ProtocolUtils.PROTOCOL_MSG_ID_MEMBER_LIST);
    AudienceInterface audienceInterface = retrofit.create(AudienceInterface.class);
    Call<String> call = audienceInterface.getAudienceList(audienceUrl,audienceObj);
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            Log.d(TAG, "onResponse");
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Log.d(TAG, "onFailure"+t.getMessage());
        }
    });
}

public interface AudienceInterface {

@POST("{url}")
Call<String>getAudienceList(@Path("url") String url,@Body JSONObject boder);

}

日志 t.getMessage 为:java.lang.NoClassDefFoundError:解析失败:Lokio/Buffer;

最佳答案

我通过添加解决了这个问题:

implementation 'com.squareup.okio:okio:2.1.0'

在 build.gradle(Module: app) 下的依赖项中。

关于retrofit - java.lang.NoClassDefFoundError : Failed resolution of: Lokio/Buffer;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995096/

相关文章:

android - 改造 API 以检索 png 图像

Kotlin - 使用 SimpleXml 解析带有列表的 Xml 响应

android - 我们有没有可能在 OkHttp 拦截器中停止请求?

android - 如何使用 Retrofit 2 处理空响应体?

android - 房间改造 Dagger2 MVP : Error: cannot find symbol variable DaggerAppComponent

android - 使用 rxandroid 调用改造 api 时刷新身份验证 token

android - 通过 IMDB API 使用改造

android - 错误 : No Retrofit annotation found.(参数 #2)

android - 使用 Retrofit 在 JSON 中上传多部分图像数据?

android - 未知根元素的改造和 SimpleXML?