java.lang.NoClassDefFoundError retrofit2.Utils

后端 未结 11 1386
梦毁少年i
梦毁少年i 2020-12-30 10:40

I\'m using Retrofit to handle the Serverside Data from Mobile. After Implementing retrofit, I am Getting the below Exception.

What am I doing wrong?

11条回答
  •  礼貌的吻别
    2020-12-30 11:18

    You should make Utils class like this:

    public class AppUtil{
    public static Retrofit getRetrofitInstance(){
            HttpLoggingInterceptor logging=new HttpLoggingInterceptor();
            if(isEnableLogging)
                logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            else
                logging.setLevel(HttpLoggingInterceptor.Level.NONE);
            Gson gson = new GsonBuilder()
                    .setExclusionStrategies(new ExclusionStrategy() {
                        @Override
                        public boolean shouldSkipField(FieldAttributes f) {
                            return f.getDeclaringClass().equals(RealmObject.class);
                        }
    
                        @Override
                        public boolean shouldSkipClass(Class clazz) {
                            return false;
                        }
                    })
                    .create();
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            httpClient.addInterceptor(logging);
    
            return new Retrofit.Builder()
                    .baseUrl(Constants.URL_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(httpClient.build())
                    .build();
    
        }
    
    
    
    }
    

    Create an interface like this for the http methods:

    public interface EndPointInterface{
     @FormUrlEncoded
        @POST(Constants.URL_LOGON)
        Call login(@Field(Constants.EMAIL) String email,
                           @Field(Constants.PASSWORD) String password);
    } 
    

    In your activity where you are calling the webservices,please proceed like this:

     @OnClick(R.id.btn_login)
        public void onLoginButtonClick() {
            String emailString = edtEmail.getText().toString().trim();
            String passwordString = edtPassword.getText().toString().trim();
    
            if (emailString.length() == 0) {
                emailWrapper.setError("Please enter E-Mail ID");
            } else if (!AppUtil.isEmailValid(emailString)) {
                emailWrapper.setError("Please enter valid E-Mail ID");
            } else if (passwordString.length() == 0) {
                passwordWrapper.setError("Please enter password");
            } else if (AppUtil.isNetworkConnectionAvailable(this, true)) {
                login(emailString,passwordString);
            }
        }
    
     private void login(String email, String pwd) {
            final MaterialDialog dialog = AppUtil.showIndeterminateProgressDialog(this,getString(R.string.please_wait));
            EndPointInterface apiService = AppUtil.getRetrofitInstance().create(EndPointInterface.class);
            Call call = apiService.login(email, pwd);
            call.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    dialog.dismiss();
                    if (response.code() == 200) {
                        Doctor doctor = response.body();
                        if (doctor == null) {
                            AppUtil.showSimpleDialog(LoginActivity.this, getString(R.string.userid_pwd_mismatched),
                                    getString(R.string.login_credential_mismatch));
                        } else {
                            SharedPreferences.Editor editor = mAppPreferences.edit();
                            editor.putString(Constants.SETTINGS_OBJ_DOCTOR, new Gson().toJson(doctor));
                            editor.putBoolean(Constants.SETTINGS_IS_LOGGED_IN, true);
                            editor.commit();
    
                            startActivity(new Intent(LoginActivity.this, PatientSummaryInfoActivity.class));
                            finish();
                        }
                    } else {
                        dialog.dismiss();
                        AppUtil.showSimpleDialog(LoginActivity.this, getString(R.string.server_error),
                                getString(R.string.could_not_connect_to_server));
                    }
    
                }
    
                @Override
                public void onFailure(Call call, Throwable t) {
                    dialog.dismiss();
                    AppUtil.showSimpleDialog(LoginActivity.this,getString(R.string.server_error), t.getMessage());
                }
    
            });
        }
    

提交回复
热议问题