表单编码的方法必须至少包含一个@Field

时间:2019-12-27 07:25:47

标签: android retrofit

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

  

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)           at $ 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();
                    }

                }

1 个答案:

答案 0 :(得分:0)

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