我想从服务器使用Post和Get方法的Retrofit库。使用proGuard
并生成APK未运行我的邮政编码时!
我的注册码
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.register_button:
name = registerName.getText().toString();
email = registerEmail.getText().toString();
password = registerPassword.getText().toString();
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
if (password.length() < 6) {
Snackbar.make(getView(), "", Snackbar.LENGTH_LONG).show();
} else {
registerLoad.setVisibility(View.VISIBLE);
registerSend.setVisibility(View.INVISIBLE);
registerProcess(name, email, password);
}
} else {
Snackbar.make(getView(), "Fill all fields", Snackbar.LENGTH_LONG).show();
}
break;
}
}
private void registerProcess(String name, String email, String password) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
OkHttpClient client = new OkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Retrofit_ApiInterface requestInterface = retrofit.create(Retrofit_ApiInterface.class);
User user = new User();
user.setName(name);
user.setEmail(email);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.REGISTER_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
try {
ServerResponse resp = response.body();
if (resp.getResult() != null && resp.getResult().equals("success")) {
Snackbar.make(getView(), StringEscapeUtils.unescapeHtml4(resp.getMessage()), Snackbar.LENGTH_LONG).show();
goToLogin();
registerLoad.setVisibility(View.INVISIBLE);
registerSend.setVisibility(View.INVISIBLE);
} else {
registerSend.setVisibility(View.VISIBLE);
Snackbar.make(getView(), StringEscapeUtils.unescapeHtml4(resp.getMessage()), Snackbar.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e("registerException", "" + e);
}
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
registerLoad.setVisibility(View.INVISIBLE);
registerSend.setVisibility(View.VISIBLE);
Snackbar.make(getView(), "Try again", Snackbar.LENGTH_LONG).show();
}
});
}
private void goToLogin() {
Fragment login = new LoginFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.loginFragment, login);
ft.commit();
toolbarTitle.setText("login");
registerFAB.setImageDrawable(getResources().getDrawable(R.drawable.ic_login_add));
}
}
我的节目:
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
使用GET
方法时没有显示错误,但使用POST
方法时显示错误!
在我的代码中,当注册“成功”或“失败”时,应该从服务器发送消息并显示在Snackbar
中。但是当使用proguard
生成APK时,Snackbar
中没有显示任何消息!
我在proguard规则中添加了以下代码,以免混淆我的模型和界面,但现在再次使用我,而不显示来自服务器的任何消息。
-dontwarn com.test.app.Retrofit.Rest.**
-dontwarn com.test.app.Retrofit.Model.User.**
如何解决此问题?请帮助我,我真的需要这个帮助
答案 0 :(得分:2)
##---------------Begin: proguard configuration for Gson ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class your.package.Models.** { *; }
# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
##---------------End: proguard configuration for Gson ----------
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
使用上述规则进行改造以与progaurd一起使用 注意:你应该在这里保留你的模型类-keep class your.package.Models。 {*; } 具有合适的名称