【问题标题】:Form-encoded method must contain at least one @Field表单编码的方法必须至少包含一个@Field
【发布时间】:2020-08-08 08:39:36
【问题描述】:

我无法理解这里的问题: 是图书馆问题吗?

java.lang.IllegalArgumentException:表单编码方法必须包含 至少一个@Field。 对于方法 ApiInterface.hitCheckVersionApi$79c79d47 在 retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720) 在 retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:11711) 在 retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166) 在 retrofit2.Retrofit$1.invoke(Retrofit.java:145) 在 java.lang.reflect.Proxy.invoke(Proxy.java:1006) 在 $Proxy3.hitCheckVersionApi$79c79d47(未知来源) 在 com.shopoholicbuddy.activities.SplashActivity.onCreate(SplashActivity.java:1058)

这里是代码示例:

  @FormUrlEncoded
    @POST("checkversion")
    Call<ResponseBody> hitCheckVersionApi(@FieldMap HashMap<String, String> map);

而实现代码为:

 private void checkForceUpdateAPI() {
        if (AppUtils.getInstance().isInternetAvailable(this)) {
            final HashMap<String, String> params = new HashMap<>();
            params.put(Constants.NetworkConstant.PARAM_VERSION_NAME, getAppVersion(this));
            params.put(Constants.NetworkConstant.PARAM_PLATFORM, "1");
            params.put(Constants.NetworkConstant.PARAM_APP_TYPE, "2");
            ApiInterface apiInterface = RestApi.createService(this, ApiInterface.class);
            Call<ResponseBody> call = apiInterface.hitCheckVersionApi(params);
            ApiCall.getInstance().hitService(this, call, new NetworkListener() {
                @Override
                public void onSuccess(int responseCode, String response, int requestCode) {
                    try {
                        VersionUpdateResponse bean = new Gson().fromJson(response, VersionUpdateResponse.class);
                        if (bean.getCode() == 310) {
                            if (bean.getResult().getVersionName() != null && Double.parseDouble(bean.getResult().getVersionName())<=(Double.parseDouble(getAppVersion(SplashActivity.this)))) {
                                showSplashScreen();
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this, R.style.DatePickerTheme);
                                builder.setTitle(getString(R.string.new_version_available));
                                builder.setMessage(getString(R.string.upgrade_to_new_version));
                                builder.setPositiveButton(getString(R.string.update), (dialogInterface, i) -> redirectStore("https://play.google.com/store/apps/details?id=com.shopoholic"));
                                if (bean.getResult().getUpdateType() != null && bean.getResult().getUpdateType().equals("1")) {
                                    builder.setNegativeButton(getString(R.string.no_thanks), (dialogInterface, i) -> showSplashScreen());
                                }
                                builder.setCancelable(false);
                                builder.show();
                            }
                        } else {
                            showSplashScreen();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        showSplashScreen();
                    }

                }

【问题讨论】:

  • 这是在调试模式还是发布模式下发生的?
  • 你能告诉我错误原因吗?
  • Apk 尚未构建
  • 如果在发布模式下出现这种情况,可能是Proguard,需要在Proguard文件中添加规则。如果它是调试模式,那么原因可能不同。
  • 请告诉我两个解决方案

标签: android retrofit


【解决方案1】:

我在 RELEASE 构建中遇到了同样的问题。添加proguard规则将解决它。 https://github.com/square/retrofit/blob/master/retrofit/src/main/resources/META-INF/proguard/retrofit2.pro 如果您在 DEBUG 构建中遇到此错误,则说明您的代码有问题。

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 2022-01-13
    • 1970-01-01
    • 2019-05-24
    • 2018-03-24
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多