为什么不在 Android 的改造中使用 POST 方法
Why not work POST method in retrofit in Android
我想为 Post 使用 Retrofit 库并从服务器获取方法。当使用 proGuard
并生成 APK 而不是 运行 我的 Post 代码!
我的注册码:
@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
方法时显示错误!
在我的代码中,当注册 "success" 或 "failure" 时,服务器应该发送消息并显示在 Snackbar
中。但是当使用 proguard
生成 APK 时,在 Snackbar
!
中不显示任何消息
我在 proguard-rules 中添加了以下代码,以不混淆我的模型和界面,但现在再次工作并且不显示来自服务器的任何消息。
-dontwarn com.test.app.Retrofit.Rest.**
-dontwarn com.test.app.Retrofit.Model.User.**
我该如何解决这个问题?请帮助我,我真的需要帮助
##---------------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 一起使用
注意:你应该在这里保留你的模型 classes -keep class your.package.Models. { *; } 有合适的名字
我想为 Post 使用 Retrofit 库并从服务器获取方法。当使用 proGuard
并生成 APK 而不是 运行 我的 Post 代码!
我的注册码:
@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
方法时显示错误!
在我的代码中,当注册 "success" 或 "failure" 时,服务器应该发送消息并显示在 Snackbar
中。但是当使用 proguard
生成 APK 时,在 Snackbar
!
我在 proguard-rules 中添加了以下代码,以不混淆我的模型和界面,但现在再次工作并且不显示来自服务器的任何消息。
-dontwarn com.test.app.Retrofit.Rest.**
-dontwarn com.test.app.Retrofit.Model.User.**
我该如何解决这个问题?请帮助我,我真的需要帮助
##---------------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 一起使用 注意:你应该在这里保留你的模型 classes -keep class your.package.Models. { *; } 有合适的名字