gpt4 book ai didi

android - 为什么不在 Android 的改造中使用 POST 方法

转载 作者:行者123 更新时间:2023-11-29 19:16:44 25 4
gpt4 key购买 nike

我想对来自服务器的 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-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一起工作注意:你应该在这里保留你的模型类 -keep class your.package.Models. { *; } 有合适的名字

关于android - 为什么不在 Android 的改造中使用 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43254404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com