gson 调试Retrofit空对象

jhdbpxl9  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(199)

我开始在我的Android应用中使用Retrofit,使用REST风格的Web服务。
我设法让它在一个简单的对象上工作。但是当尝试使用一个更复杂的对象时,它仍然是非常空的。我把Okhttp放在debug中,我所期望的json出现在响应中,所以我认为在创建过程中有问题(或填充)。那么调试这种情况的最好方法是什么呢?我试过设置断点(不确定放在哪里),但什么也没有发现。
代码中有趣的部分在这里

ClientRequest clientRequest = new ClientRequest(sessionId, 1);
apiInterface = APIClient.getClient().create(APIInterface.class);

Call userCall = apiInterface.getUser(clientRequest);
userCall.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {

if (response.isSuccessful()) {
    Client client = (Client) response.body();

    Log.d("TAG Request", call.request().toString() + "");
    Log.d("TAG Response Code ", response.code() + "");
    Log.d("TAG Response Body ", response.body() + "");
    Log.d("TAG Response Message ", response.message() + "");
    Log.d("TAG Id ",client.getClientId() + "");
    Log.d("TAG Nom de l'Entreprise", client.getCompanyName() + "");
    Log.d("TAG Nom du Contact", client.getContactName() + "");
    Log.d("TAG Numéro client", client.getCustomerNo() + "");
    Log.d("TAG Nom d'utilisateur", client.getUsername() + "");
    Log.d("TAG Ville", client.getCity() + "");
    Log.d("TAG Pays", client.getCountry() + "");

    } else {
        Log.d("error message", response.errorBody().toString());
    }
}

@Override
public void onFailure(Call call, Throwable t) {
    call.cancel();
}
});

API接口(登录工作方式)

public interface APIInterface {

    //@FormUrlEncoded
    @POST("/remote/json.php?login")
    Call<Token> doLogin(@Body Credential credential);

    @POST("/remote/json.php?client_get")
    Call<Client> getUser(@Body ClientRequest clientRequest);
}

http://www.jsonschema2pojo.org/生成的客户端类

public class Client {

@SerializedName("client_id")
@Expose
public String clientId;
@SerializedName("sys_userid")
@Expose
public String sysUserid;
@SerializedName("sys_groupid")
@Expose
public String sysGroupid;
@SerializedName("sys_perm_user")
@Expose
public String sysPermUser;
@SerializedName("sys_perm_group")
@Expose
public String sysPermGroup;
@SerializedName("sys_perm_other")
@Expose
public String sysPermOther;
@SerializedName("company_name")
@Expose
public String companyName;
@SerializedName("company_id")
@Expose
public String companyId;
@SerializedName("gender")
@Expose
public String gender;
@SerializedName("contact_firstname")
@Expose
public String contactFirstname;
@SerializedName("contact_name")
@Expose
public String contactName;
@SerializedName("customer_no")
@Expose
public String customerNo;
@SerializedName("vat_id")
@Expose
public String vatId;
@SerializedName("street")
@Expose
public String street;
@SerializedName("zip")
@Expose
public String zip;
@SerializedName("city")
@Expose
public String city;
@SerializedName("state")
@Expose
public String state;
@SerializedName("country")
@Expose
public String country;
@SerializedName("telephone")
@Expose
public String telephone;
@SerializedName("mobile")
@Expose
public String mobile;
@SerializedName("fax")
@Expose
public String fax;
@SerializedName("email")
@Expose
public String email;
@SerializedName("internet")
@Expose
public String internet;
@SerializedName("icq")
@Expose
public String icq;
@SerializedName("notes")
@Expose
public String notes;
@SerializedName("bank_account_owner")
@Expose
public String bankAccountOwner;
@SerializedName("bank_account_number")
@Expose
public String bankAccountNumber;
@SerializedName("bank_code")
@Expose
public String bankCode;
@SerializedName("bank_name")
@Expose
public String bankName;
@SerializedName("bank_account_iban")
@Expose
public String bankAccountIban;
@SerializedName("bank_account_swift")
@Expose
public String bankAccountSwift;
@SerializedName("paypal_email")
@Expose
public String paypalEmail;
@SerializedName("default_mailserver")
@Expose
public String defaultMailserver;
@SerializedName("mail_servers")
@Expose
public String mailServers;
@SerializedName("limit_maildomain")
@Expose
public String limitMaildomain;
@SerializedName("limit_mailbox")
@Expose
public String limitMailbox;
@SerializedName("limit_mailalias")
@Expose
public String limitMailalias;
@SerializedName("limit_mailaliasdomain")
@Expose
public String limitMailaliasdomain;
@SerializedName("limit_mailforward")
@Expose
public String limitMailforward;
@SerializedName("limit_mailcatchall")
@Expose
public String limitMailcatchall;
@SerializedName("limit_mailrouting")
@Expose
public String limitMailrouting;
@SerializedName("limit_mailfilter")
@Expose
public String limitMailfilter;
@SerializedName("limit_fetchmail")
@Expose
public String limitFetchmail;
@SerializedName("limit_mailquota")
@Expose
public String limitMailquota;
@SerializedName("limit_spamfilter_wblist")
@Expose
public String limitSpamfilterWblist;
@SerializedName("limit_spamfilter_user")
@Expose
public String limitSpamfilterUser;
@SerializedName("limit_spamfilter_policy")
@Expose
public String limitSpamfilterPolicy;
@SerializedName("default_webserver")
@Expose
public String defaultWebserver;
@SerializedName("web_servers")
@Expose
public String webServers;
@SerializedName("limit_web_ip")
@Expose
public String limitWebIp;
@SerializedName("limit_web_domain")
@Expose
public String limitWebDomain;
@SerializedName("limit_web_quota")
@Expose
public String limitWebQuota;
@SerializedName("web_php_options")
@Expose
public String webPhpOptions;
@SerializedName("limit_cgi")
@Expose
public String limitCgi;
@SerializedName("limit_ssi")
@Expose
public String limitSsi;
@SerializedName("limit_perl")
@Expose
public String limitPerl;
@SerializedName("limit_ruby")
@Expose
public String limitRuby;
@SerializedName("limit_python")
@Expose
public String limitPython;
@SerializedName("force_suexec")
@Expose
public String forceSuexec;
@SerializedName("limit_hterror")
@Expose
public String limitHterror;
@SerializedName("limit_wildcard")
@Expose
public String limitWildcard;
@SerializedName("limit_ssl")
@Expose
public String limitSsl;
@SerializedName("limit_ssl_letsencrypt")
@Expose
public String limitSslLetsencrypt;
@SerializedName("limit_web_subdomain")
@Expose
public String limitWebSubdomain;
@SerializedName("limit_web_aliasdomain")
@Expose
public String limitWebAliasdomain;
@SerializedName("limit_ftp_user")
@Expose
public String limitFtpUser;
@SerializedName("limit_shell_user")
@Expose
public String limitShellUser;
@SerializedName("ssh_chroot")
@Expose
public String sshChroot;
@SerializedName("limit_webdav_user")
@Expose
public String limitWebdavUser;
@SerializedName("limit_backup")
@Expose
public String limitBackup;
@SerializedName("limit_directive_snippets")
@Expose
public String limitDirectiveSnippets;
@SerializedName("limit_aps")
@Expose
public String limitAps;
@SerializedName("default_dnsserver")
@Expose
public String defaultDnsserver;
@SerializedName("db_servers")
@Expose
public String dbServers;
@SerializedName("limit_dns_zone")
@Expose
public String limitDnsZone;
@SerializedName("default_slave_dnsserver")
@Expose
public String defaultSlaveDnsserver;
@SerializedName("limit_dns_slave_zone")
@Expose
public String limitDnsSlaveZone;
@SerializedName("limit_dns_record")
@Expose
public String limitDnsRecord;
@SerializedName("default_dbserver")
@Expose
public String defaultDbserver;
@SerializedName("dns_servers")
@Expose
public String dnsServers;
@SerializedName("limit_database")
@Expose
public String limitDatabase;
@SerializedName("limit_database_user")
@Expose
public String limitDatabaseUser;
@SerializedName("limit_database_quota")
@Expose
public String limitDatabaseQuota;
@SerializedName("limit_cron")
@Expose
public String limitCron;
@SerializedName("limit_cron_type")
@Expose
public String limitCronType;
@SerializedName("limit_cron_frequency")
@Expose
public String limitCronFrequency;
@SerializedName("limit_traffic_quota")
@Expose
public String limitTrafficQuota;
@SerializedName("limit_client")
@Expose
public String limitClient;
@SerializedName("limit_domainmodule")
@Expose
public String limitDomainmodule;
@SerializedName("limit_mailmailinglist")
@Expose
public String limitMailmailinglist;
@SerializedName("limit_openvz_vm")
@Expose
public String limitOpenvzVm;
@SerializedName("limit_openvz_vm_template_id")
@Expose
public String limitOpenvzVmTemplateId;
@SerializedName("parent_client_id")
@Expose
public String parentClientId;
@SerializedName("username")
@Expose
public String username;
@SerializedName("password")
@Expose
public String password;
@SerializedName("language")
@Expose
public String language;
@SerializedName("usertheme")
@Expose
public String usertheme;
@SerializedName("template_master")
@Expose
public String templateMaster;
@SerializedName("template_additional")
@Expose
public String templateAdditional;
@SerializedName("created_at")
@Expose
public String createdAt;
@SerializedName("locked")
@Expose
public String locked;
@SerializedName("canceled")
@Expose
public String canceled;
@SerializedName("can_use_api")
@Expose
public String canUseApi;
@SerializedName("tmp_data")
@Expose
public String tmpData;
@SerializedName("id_rsa")
@Expose
public String idRsa;
@SerializedName("ssh_rsa")
@Expose
public String sshRsa;
@SerializedName("customer_no_template")
@Expose
public String customerNoTemplate;
@SerializedName("customer_no_start")
@Expose
public String customerNoStart;
@SerializedName("customer_no_counter")
@Expose
public String customerNoCounter;
@SerializedName("added_date")
@Expose
public String addedDate;
@SerializedName("added_by")
@Expose
public String addedBy;
@SerializedName("default_xmppserver")
@Expose
public String defaultXmppserver;
@SerializedName("xmpp_servers")
@Expose
public String xmppServers;
@SerializedName("limit_xmpp_domain")
@Expose
public String limitXmppDomain;
@SerializedName("limit_xmpp_user")
@Expose
public String limitXmppUser;
@SerializedName("limit_xmpp_muc")
@Expose
public String limitXmppMuc;
@SerializedName("limit_xmpp_anon")
@Expose
public String limitXmppAnon;
@SerializedName("limit_xmpp_auth_options")
@Expose
public String limitXmppAuthOptions;
@SerializedName("limit_xmpp_vjud")
@Expose
public String limitXmppVjud;
@SerializedName("limit_xmpp_proxy")
@Expose
public String limitXmppProxy;
@SerializedName("limit_xmpp_status")
@Expose
public String limitXmppStatus;
@SerializedName("limit_xmpp_pastebin")
@Expose
public String limitXmppPastebin;
@SerializedName("limit_xmpp_httparchive")
@Expose
public String limitXmppHttparchive;

public Client() {

    Log.d("TAG","Default Constructor");

}

public String getClientId() {
    return clientId;
}

public String getSysUserid() {
    return sysUserid;
}

public String getSysGroupid() {
    return sysGroupid;
}

public String getSysPermUser() {
    return sysPermUser;
}

public String getSysPermGroup() {
    return sysPermGroup;
}

public String getSysPermOther() {
    return sysPermOther;
}

public String getCompanyName() {
    return companyName;
}

public String getCompanyId() {
    return companyId;
}

public String getGender() {
    return gender;
}

public String getContactFirstname() {
    return contactFirstname;
}

public String getContactName() {
    return contactName;
}

public String getCustomerNo() {
    return customerNo;
}

public String getVatId() {
    return vatId;
}

public String getStreet() {
    return street;
}

public String getZip() {
    return zip;
}

public String getCity() {
    return city;
}

public String getState() {
    return state;
}

public String getCountry() {
    return country;
}

public String getTelephone() {
    return telephone;
}

public String getMobile() {
    return mobile;
}

public String getFax() {
    return fax;
}

public String getEmail() {
    return email;
}

public String getInternet() {
    return internet;
}

public String getIcq() {
    return icq;
}

public String getNotes() {
    return notes;
}

public String getBankAccountOwner() {
    return bankAccountOwner;
}

public String getBankAccountNumber() {
    return bankAccountNumber;
}

public String getBankCode() {
    return bankCode;
}

public String getBankName() {
    return bankName;
}

public String getBankAccountIban() {
    return bankAccountIban;
}

public String getBankAccountSwift() {
    return bankAccountSwift;
}

public String getPaypalEmail() {
    return paypalEmail;
}

public String getDefaultMailserver() {
    return defaultMailserver;
}

public String getMailServers() {
    return mailServers;
}

public String getLimitMaildomain() {
    return limitMaildomain;
}

public String getLimitMailbox() {
    return limitMailbox;
}

public String getLimitMailalias() {
    return limitMailalias;
}

public String getLimitMailaliasdomain() {
    return limitMailaliasdomain;
}

public String getLimitMailforward() {
    return limitMailforward;
}

public String getLimitMailcatchall() {
    return limitMailcatchall;
}

public String getLimitMailrouting() {
    return limitMailrouting;
}

public String getLimitMailfilter() {
    return limitMailfilter;
}

public String getLimitFetchmail() {
    return limitFetchmail;
}

public String getLimitMailquota() {
    return limitMailquota;
}

public String getLimitSpamfilterWblist() {
    return limitSpamfilterWblist;
}

public String getLimitSpamfilterUser() {
    return limitSpamfilterUser;
}

public String getLimitSpamfilterPolicy() {
    return limitSpamfilterPolicy;
}

public String getDefaultWebserver() {
    return defaultWebserver;
}

public String getWebServers() {
    return webServers;
}

public String getLimitWebIp() {
    return limitWebIp;
}

public String getLimitWebDomain() {
    return limitWebDomain;
}

public String getLimitWebQuota() {
    return limitWebQuota;
}

public String getWebPhpOptions() {
    return webPhpOptions;
}

public String getLimitCgi() {
    return limitCgi;
}

public String getLimitSsi() {
    return limitSsi;
}

public String getLimitPerl() {
    return limitPerl;
}

public String getLimitRuby() {
    return limitRuby;
}

public String getLimitPython() {
    return limitPython;
}

public String getForceSuexec() {
    return forceSuexec;
}

public String getLimitHterror() {
    return limitHterror;
}

public String getLimitWildcard() {
    return limitWildcard;
}

public String getLimitSsl() {
    return limitSsl;
}

public String getLimitSslLetsencrypt() {
    return limitSslLetsencrypt;
}

public String getLimitWebSubdomain() {
    return limitWebSubdomain;
}

public String getLimitWebAliasdomain() {
    return limitWebAliasdomain;
}

public String getLimitFtpUser() {
    return limitFtpUser;
}

public String getLimitShellUser() {
    return limitShellUser;
}

public String getSshChroot() {
    return sshChroot;
}

public String getLimitWebdavUser() {
    return limitWebdavUser;
}

public String getLimitBackup() {
    return limitBackup;
}

public String getLimitDirectiveSnippets() {
    return limitDirectiveSnippets;
}

public String getLimitAps() {
    return limitAps;
}

public String getDefaultDnsserver() {
    return defaultDnsserver;
}

public String getDbServers() {
    return dbServers;
}

public String getLimitDnsZone() {
    return limitDnsZone;
}

public String getDefaultSlaveDnsserver() {
    return defaultSlaveDnsserver;
}

public String getLimitDnsSlaveZone() {
    return limitDnsSlaveZone;
}

public String getLimitDnsRecord() {
    return limitDnsRecord;
}

public String getDefaultDbserver() {
    return defaultDbserver;
}

public String getDnsServers() {
    return dnsServers;
}

public String getLimitDatabase() {
    return limitDatabase;
}

public String getLimitDatabaseUser() {
    return limitDatabaseUser;
}

public String getLimitDatabaseQuota() {
    return limitDatabaseQuota;
}

public String getLimitCron() {
    return limitCron;
}

public String getLimitCronType() {
    return limitCronType;
}

public String getLimitCronFrequency() {
    return limitCronFrequency;
}

public String getLimitTrafficQuota() {
    return limitTrafficQuota;
}

public String getLimitClient() {
    return limitClient;
}

public String getLimitDomainmodule() {
    return limitDomainmodule;
}

public String getLimitMailmailinglist() {
    return limitMailmailinglist;
}

public String getLimitOpenvzVm() {
    return limitOpenvzVm;
}

public String getLimitOpenvzVmTemplateId() {
    return limitOpenvzVmTemplateId;
}

public String getParentClientId() {
    return parentClientId;
}

public String getUsername() {
    return username;
}

public String getPassword() {
    return password;
}

public String getLanguage() {
    return language;
}

public String getUsertheme() {
    return usertheme;
}

public String getTemplateMaster() {
    return templateMaster;
}

public String getTemplateAdditional() {
    return templateAdditional;
}

public String getCreatedAt() {
    return createdAt;
}

public String getLocked() {
    return locked;
}

public String getCanceled() {
    return canceled;
}

public String getCanUseApi() {
    return canUseApi;
}

public String getTmpData() {
    return tmpData;
}

public String getIdRsa() {
    return idRsa;
}

public String getSshRsa() {
    return sshRsa;
}

public String getCustomerNoTemplate() {
    return customerNoTemplate;
}

public String getCustomerNoStart() {
    return customerNoStart;
}

public String getCustomerNoCounter() {
    return customerNoCounter;
}

public String getAddedDate() {
    return addedDate;
}

public String getAddedBy() {
    return addedBy;
}

public String getDefaultXmppserver() {
    return defaultXmppserver;
}

public String getXmppServers() {
    return xmppServers;
}

public String getLimitXmppDomain() {
    return limitXmppDomain;
}

public String getLimitXmppUser() {
    return limitXmppUser;
}

public String getLimitXmppMuc() {
    return limitXmppMuc;
}

public String getLimitXmppAnon() {
    return limitXmppAnon;
}

public String getLimitXmppAuthOptions() {
    return limitXmppAuthOptions;
}

public String getLimitXmppVjud() {
    return limitXmppVjud;
}

public String getLimitXmppProxy() {
    return limitXmppProxy;
}

public String getLimitXmppStatus() {
    return limitXmppStatus;
}

public String getLimitXmppPastebin() {
    return limitXmppPastebin;
}

public String getLimitXmppHttparchive() {
    return limitXmppHttparchive;
}
}
xuo3flqw

xuo3flqw1#

首先尝试使用Postman等客户端向WebServices发送数据,这是一个非常有用的工具。您可以查看WebService向Android应用发送了什么

ibps3vxo

ibps3vxo2#

我在Kotlin上使用改型时也遇到了同样的问题。
它是:

data class MtsApprovalResponseData(

    @field:Element(name = "MessageId", required = false)
    @Namespace(reference = WORKAROUND_NAMESPACE)
    var messageId: String? = null,

    @field:Element(name = "Version", required = false)
    @Namespace(reference = WORKAROUND_NAMESPACE)
    var version: String? = null

)

并且如果通过移动到小名称参数来修复空值:

data class MtsApprovalResponseData(

    @field:Element(name = "messageId", required = false)
    @Namespace(reference = WORKAROUND_NAMESPACE)
    var messageId: String? = null,

    @field:Element(name = "version", required = false)
    @Namespace(reference = WORKAROUND_NAMESPACE)
    var version: String? = null

)

顺便说一句,我改造豆是:

@Bean
    fun soapRetrofit(): Retrofit {
        val logging = HttpLoggingInterceptor()
        logging.level = HttpLoggingInterceptor.Level.BODY
        val httpClient = OkHttpClient.Builder().addInterceptor(logging).build()

        return Retrofit.Builder()
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .baseUrl(profile.serviceApiUrl)
            .client(httpClient)
            .build()
    }

相关问题