Runtime Execption java.lang.NoClassDefFoundError: retrofit2.Platform in Android

后端 未结 3 971
小鲜肉
小鲜肉 2021-01-13 09:33

i am using Rtofit to handling the Serverside Data from Mobile After Implementing the Retrofit I am Getting the below Exception any know about this issue tell me where i am d

相关标签:
3条回答
  • 2021-01-13 09:54

    Try change the okhttp to okhttp3

    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
    

    As retrofit2 internally using okhttp3.

    Try build and sync gradle.

    0 讨论(0)
  • 2021-01-13 10:00

    I had the same problem. Possible solution (fixed my problem):

    1) Add gradle dependency: compile 'com.android.support:multidex:1.0.0' (file build.gradle)

    2) Set multiDexEnabled true (file build.gradle)

    3) Add android:name="android.support.multidex.MultiDexApplication" (file AndroidManifest.xml)

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="android.support.multidex.MultiDexApplication">
     </application>
    
    0 讨论(0)
  • 2021-01-13 10:08

    Add these to your Service class

    //create OkHttpClient client

        Strategy strategy = new AnnotationStrategy();
    
        // Define the interceptor, add authentication headers
        Interceptor interceptor = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder().addHeader("User-Agent", "Retrofit-Sample-App").build();
                return chain.proceed(newRequest);
            }
        };
    
    
        Serializer serializer = new Persister(strategy);
    
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.interceptors().add(interceptor);
        OkHttpClient client = builder.build();
    
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
    
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.networkInterceptors().add(httpLoggingInterceptor);
        builder.build();
    
        RxJavaCallAdapterFactory rxAdapter = RxJavaCallAdapterFactory.create();
    
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(SimpleXmlConverterFactory.create(serializer))
                .addCallAdapterFactory(rxAdapter)
                .baseUrl(YellowTalkConstant.BASE_URL)
                .client(client)
                .build();
    
        try{
            this.mYellowTalkApi = retrofit.create(YellowTalkApi.class);
        }catch (Exception ex) {
            Log.e("Exception", ex.toString());
        }
    
    0 讨论(0)
提交回复
热议问题