java.lang.NoClassDefFoundError retrofit2.Utils
java.lang.NoClassDefFoundError retrofit2.Utils
我正在使用 Retrofit 处理来自移动设备的服务器端数据。实施改造后,我得到以下异常。
我做错了什么?
com.name.App_idea W/System.err: java.lang.NoClassDefFoundError:
retrofit2.Utils
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:434)
at com.name.App_idea.utils.Idea.onCreate(Idea.java:103)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4541)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1381)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
改装初始化
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
Gradle 文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
applicationId "com.name.App_idea"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "9"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
compile 'com.android.support:support-v4:23.3.0'
}
申请Class
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.util.Log;
import java.io.File;
import java.security.cert.CertificateException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class Idea extends Application {
public static Retrofit mRetrofit;
public static IdeaService Iservice;
public static LoginResponceModel loinResponce;
public static SettingsModel settingModel;
public static LocationModel location = new LocationModel();
private static SQLiteDatabase dbase;
private static String FILE_PATH;
public static SQLiteDatabase getDataBase() {
return dbase;
}
public static String getFilePath() {
return FILE_PATH;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, "App", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(AppConstance.DbConstans.tblLogin);
Log.i("DB", "Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
public static void deleteAllTables() {
getDataBase().execSQL("DELETE FROM login");
}
@Override
public void onCreate() {
super.onCreate();
try {
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
Iservice = mRetrofit.create(IdeaService.class);
MultiDex.install(this);
DatabaseHelper dbHelper = new DatabaseHelper(this);
dbase = dbHelper.getWritableDatabase();
AppDataService appDataService = new AppDataService();
loinResponce = appDataService.getLoginDetails();
settingModel = appDataService.getSettings();
FILE_PATH = getAppFilePath();
startService(new Intent(Idea.this, LocationTracker.class));
} catch (Exception e) {
e.printStackTrace();
}
}
public String combineFilePath(String path1, String path2) {
File file1 = new File(path1);
File file2 = new File(file1, path2);
return file2.getPath();
}
public String getAppFilePath() {
String dsPath;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
dsPath = combineFilePath(Environment
.getExternalStorageDirectory().getAbsolutePath(),
"android/data/Idea/");
else
dsPath = this.getDir(
this.getPackageName(), 0).getAbsolutePath();
new File(dsPath).mkdirs();
return dsPath;
}
private OkHttpClient getOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
重写到
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build();
你应该像这样制作 Utils class:
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();
}
}
为 http 方法创建这样的接口:
public interface EndPointInterface{
@FormUrlEncoded
@POST(Constants.URL_LOGON)
Call<Doctor> login(@Field(Constants.EMAIL) String email,
@Field(Constants.PASSWORD) String password);
}
在您调用网络服务的 activity 中,请按如下方式进行:
@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<Doctor> call = apiService.login(email, pwd);
call.enqueue(new Callback<Doctor>() {
@Override
public void onResponse(Call<Doctor> call, Response<Doctor> 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<Doctor> call, Throwable t) {
dialog.dismiss();
AppUtil.showSimpleDialog(LoginActivity.this,getString(R.string.server_error), t.getMessage());
}
});
}
有了这么多库,它肯定已经超过了臭名昭著的 64k 方法限制,您需要为您的应用启用 multidex。这是你如何做的。 https://developer.android.com/tools/building/multidex.html
您是否添加了以下 Proguard 规则?
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
尝试对基本改造库和转换器使用相同的库版本。
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-scalars:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
由于 MultiDexApplication 会出现此错误。我遇到过与其他一些库不同的库的此类问题,但其他一些 library.It 会因应用程序初始化而导致改造库出错在dex(你的retrofit库代码转换成dex)文件没有设置(安装)的地方启动。
要解决您需要处理多个 Dex 文件的问题。在应用程序 build.gradle
和应用程序 class
的帮助下
build.gradle
文件
中要求的以下更改
dexOptions {
incremental true
// here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY
javaMaxHeapSize "4g"
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
// your dependencies which you are using.
}
整个build.gradle
android {
signingConfigs {
/*
releasebuild {
keyAlias 'hellotest'
keyPassword 'hellotest'
storeFile file('path to keystore')
storePassword 'hellotest'
}
*/
}
compileSdkVersion 'Google Inc.:Google APIs:22'
buildToolsVersion '23.0.0'
/* if you got error regarding duplicate file of META-INF/LICENSE.txt from jar file
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
*/
dexOptions {
jumboMode = true
incremental true
// here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY
javaMaxHeapSize "4g"
}
defaultConfig {
multiDexEnabled true
applicationId "com.myapp.packagenme"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releasebuild
}
debug {
signingConfig signingConfigs.releasebuild
}
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
// your dependencies which you are using.
}
如果您的应用使用扩展应用程序class,您可以覆盖attachBaseContext()
方法并调用MultiDex.install(this)
来启用multidex。 To install multipledex file context using Applicaiton class 应扩展 [MultiDexApplication][2]
public class MyAppClass extends MultiDexApplication{
@Override
protected void attachBaseContext(Context newBase) {
MultiDex.install(newBase);
super.attachBaseContext(newBase);
}
}
建议
Selectively compiling APIs into your executable
不要使用整个 google 播放服务,只使用需要的 library.From 版本 6.5,您可以有选择地将 Google 播放服务 API 编译到您的应用程序中。例如,要仅包含 Google Fit 和 Android Wear API,请在 build.gradle 文件中替换以下行:
compile 'com.google.android.gms:play-services:8.4.0'
这些行:
compile 'com.google.android.gms:play-services-fitness:8.4.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
参考我的回答
这是 Gradle 构建配置问题 我有时也遇到同样的问题
在Gradle你使用
compile 'com.google.android.gms:play-services:8.4.0'
Enire Google Playservice Lib 可以改成你项目中使用的lib吗
例子
com.google.android.gms:play-services-gcm:8.4.0
com.google.android.gms:play-services-maps:8.4.0
com.google.android.gms:play-services-auth:8.4.0
参考这个 URl https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project
我运行遇到类似的问题,这可能是一个依赖问题
compile 'com.squareup.retrofit2:retrofit:2.0.2'
已经包含 okhttp,如此处所示https://github.com/square/retrofit/blob/d04f3a50e41ca01d22f370ac4f332f6ddf4ba9fe/pom.xml,因此从依赖项列表中删除 okhttp,然后同步并重试。
我遇到了这个错误。经过多次搜索,发现我忘记添加 Internet Permission to manifest。
添加此权限后,我的错误已解决。
我在棒棒糖设备下遇到了这个错误。
我使用的是 multiDexEnabled true。
将此代码添加到 class 扩展应用程序后 class 我的问题解决了。
@Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
MultiDex.install(this);
}
参考:
我通过将改造 gradle 版本 2.7.0 更改为 2.5.0
来解决这个问题
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
如果未添加改造依赖项或未添加任何其他 google 服务,您可能会遇到此类问题。如果添加了 Internet 权限,也请始终检查清单文件
在清单文件中
<uses-permission android:name="android.permission.INTERNET" />
在应用级别 gradle
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
我正在使用 Retrofit 处理来自移动设备的服务器端数据。实施改造后,我得到以下异常。
我做错了什么?
com.name.App_idea W/System.err: java.lang.NoClassDefFoundError: retrofit2.Utils at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:434) at com.name.App_idea.utils.Idea.onCreate(Idea.java:103) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4541) at android.app.ActivityThread.access00(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1381) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5292) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) at dalvik.system.NativeStart.main(Native Method)
改装初始化
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
Gradle 文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
applicationId "com.name.App_idea"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "9"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-scalars:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
compile 'com.android.support:support-v4:23.3.0'
}
申请Class
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.util.Log;
import java.io.File;
import java.security.cert.CertificateException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class Idea extends Application {
public static Retrofit mRetrofit;
public static IdeaService Iservice;
public static LoginResponceModel loinResponce;
public static SettingsModel settingModel;
public static LocationModel location = new LocationModel();
private static SQLiteDatabase dbase;
private static String FILE_PATH;
public static SQLiteDatabase getDataBase() {
return dbase;
}
public static String getFilePath() {
return FILE_PATH;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, "App", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(AppConstance.DbConstans.tblLogin);
Log.i("DB", "Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
public static void deleteAllTables() {
getDataBase().execSQL("DELETE FROM login");
}
@Override
public void onCreate() {
super.onCreate();
try {
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
Iservice = mRetrofit.create(IdeaService.class);
MultiDex.install(this);
DatabaseHelper dbHelper = new DatabaseHelper(this);
dbase = dbHelper.getWritableDatabase();
AppDataService appDataService = new AppDataService();
loinResponce = appDataService.getLoginDetails();
settingModel = appDataService.getSettings();
FILE_PATH = getAppFilePath();
startService(new Intent(Idea.this, LocationTracker.class));
} catch (Exception e) {
e.printStackTrace();
}
}
public String combineFilePath(String path1, String path2) {
File file1 = new File(path1);
File file2 = new File(file1, path2);
return file2.getPath();
}
public String getAppFilePath() {
String dsPath;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
dsPath = combineFilePath(Environment
.getExternalStorageDirectory().getAbsolutePath(),
"android/data/Idea/");
else
dsPath = this.getDir(
this.getPackageName(), 0).getAbsolutePath();
new File(dsPath).mkdirs();
return dsPath;
}
private OkHttpClient getOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
重写到
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
mRetrofit = new Retrofit.Builder()
.baseUrl(AppConstance.APP_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build();
你应该像这样制作 Utils class:
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();
}
}
为 http 方法创建这样的接口:
public interface EndPointInterface{
@FormUrlEncoded
@POST(Constants.URL_LOGON)
Call<Doctor> login(@Field(Constants.EMAIL) String email,
@Field(Constants.PASSWORD) String password);
}
在您调用网络服务的 activity 中,请按如下方式进行:
@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<Doctor> call = apiService.login(email, pwd);
call.enqueue(new Callback<Doctor>() {
@Override
public void onResponse(Call<Doctor> call, Response<Doctor> 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<Doctor> call, Throwable t) {
dialog.dismiss();
AppUtil.showSimpleDialog(LoginActivity.this,getString(R.string.server_error), t.getMessage());
}
});
}
有了这么多库,它肯定已经超过了臭名昭著的 64k 方法限制,您需要为您的应用启用 multidex。这是你如何做的。 https://developer.android.com/tools/building/multidex.html
您是否添加了以下 Proguard 规则?
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
尝试对基本改造库和转换器使用相同的库版本。
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-scalars:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
由于 MultiDexApplication 会出现此错误。我遇到过与其他一些库不同的库的此类问题,但其他一些 library.It 会因应用程序初始化而导致改造库出错在dex(你的retrofit库代码转换成dex)文件没有设置(安装)的地方启动。
要解决您需要处理多个 Dex 文件的问题。在应用程序 build.gradle
和应用程序 class
build.gradle
文件
dexOptions {
incremental true
// here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY
javaMaxHeapSize "4g"
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
// your dependencies which you are using.
}
整个build.gradle
android {
signingConfigs {
/*
releasebuild {
keyAlias 'hellotest'
keyPassword 'hellotest'
storeFile file('path to keystore')
storePassword 'hellotest'
}
*/
}
compileSdkVersion 'Google Inc.:Google APIs:22'
buildToolsVersion '23.0.0'
/* if you got error regarding duplicate file of META-INF/LICENSE.txt from jar file
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
*/
dexOptions {
jumboMode = true
incremental true
// here heap size give 4g i got this thing from https://groups.google.com/forum/#!topic/adt-dev/P_TLBTyFWVY
javaMaxHeapSize "4g"
}
defaultConfig {
multiDexEnabled true
applicationId "com.myapp.packagenme"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releasebuild
}
debug {
signingConfig signingConfigs.releasebuild
}
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
// your dependencies which you are using.
}
如果您的应用使用扩展应用程序class,您可以覆盖attachBaseContext()
方法并调用MultiDex.install(this)
来启用multidex。 To install multipledex file context using Applicaiton class 应扩展 [MultiDexApplication][2]
public class MyAppClass extends MultiDexApplication{
@Override
protected void attachBaseContext(Context newBase) {
MultiDex.install(newBase);
super.attachBaseContext(newBase);
}
}
建议
Selectively compiling APIs into your executable
不要使用整个 google 播放服务,只使用需要的 library.From 版本 6.5,您可以有选择地将 Google 播放服务 API 编译到您的应用程序中。例如,要仅包含 Google Fit 和 Android Wear API,请在 build.gradle 文件中替换以下行:
compile 'com.google.android.gms:play-services:8.4.0'
这些行:
compile 'com.google.android.gms:play-services-fitness:8.4.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
参考我的回答
这是 Gradle 构建配置问题 我有时也遇到同样的问题
在Gradle你使用
compile 'com.google.android.gms:play-services:8.4.0'
Enire Google Playservice Lib 可以改成你项目中使用的lib吗
例子
com.google.android.gms:play-services-gcm:8.4.0
com.google.android.gms:play-services-maps:8.4.0
com.google.android.gms:play-services-auth:8.4.0
参考这个 URl https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project
我运行遇到类似的问题,这可能是一个依赖问题
compile 'com.squareup.retrofit2:retrofit:2.0.2'
已经包含 okhttp,如此处所示https://github.com/square/retrofit/blob/d04f3a50e41ca01d22f370ac4f332f6ddf4ba9fe/pom.xml,因此从依赖项列表中删除 okhttp,然后同步并重试。
我遇到了这个错误。经过多次搜索,发现我忘记添加 Internet Permission to manifest。
添加此权限后,我的错误已解决。
我在棒棒糖设备下遇到了这个错误。
我使用的是 multiDexEnabled true。
将此代码添加到 class 扩展应用程序后 class 我的问题解决了。
@Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
MultiDex.install(this);
}
参考:
我通过将改造 gradle 版本 2.7.0 更改为 2.5.0
来解决这个问题implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
如果未添加改造依赖项或未添加任何其他 google 服务,您可能会遇到此类问题。如果添加了 Internet 权限,也请始终检查清单文件
在清单文件中
<uses-permission android:name="android.permission.INTERNET" />
在应用级别 gradle
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'