Android 使用安卓系统中的改型将数据发布到服务器

Android 使用安卓系统中的改型将数据发布到服务器,android,post,retrofit2,Android,Post,Retrofit2,我曾尝试使用改型将此数据发布到服务器。 所以我尝试创建序列化请求来发布这些数据。 通常,如果这个请求 { "FirstName":"test", "LastName":"test", "EmailAddress":"test@test.com", "Password":"test", "DeviceToken":"", "PushToken":"", "DeviceType":"test", "SMSSubscribe":"1", "EmailSubscribe":"1", "TermsCond

我曾尝试使用改型将此数据发布到服务器。 所以我尝试创建序列化请求来发布这些数据。 通常,如果这个请求

{
"FirstName":"test",
"LastName":"test",
"EmailAddress":"test@test.com",
"Password":"test",
"DeviceToken":"",
"PushToken":"",
"DeviceType":"test",
"SMSSubscribe":"1",
"EmailSubscribe":"1",
"TermsCondition":"1",
}
然后我像这样创建了序列化类

public class RegisterRequest {

@SerializedName("DeviceToken")
public String deviceToken;

@SerializedName("PushToken")
public String pushToken;

@SerializedName("AppVersion")
public String appVersion;

@SerializedName("DevicePlatform")
public String devicePlatform;

@SerializedName("DeviceType")
public String deviceType;

@SerializedName("FirstName")
public String firstName;

@SerializedName("LastName")
public String lastName;

@SerializedName("EmailAddress")
public String email;

@SerializedName("Password")
public String password;

@SerializedName("MobileCountryCode")
public String mobileCountryCode;

@SerializedName("MobileNumber")
public String mobileNumber;

@SerializedName("SMSSubscribe")
public String smsSubscribe;

@SerializedName("EmailSubscribe")
public String emailSubscribe;

@SerializedName("TermsCondition")
public String termsCondition;

@SerializedName("DeviceInformation")
public String deviceInformation;
 final RegisterRequest registerRequest = new RegisterRequest();
    registerRequest.firstName = givenName;
    registerRequest.lastName = familyName;
    registerRequest.email = email;
    registerRequest.password = password;
    registerRequest.deviceToken = Repository.getDeviceId();
    registerRequest.pushToken = "";
    registerRequest.deviceType = Repository.DEVICE_TYPE;
    registerRequest.appVersion = Repository.APP_VERSION;
    registerRequest.devicePlatform = Repository.DEVICE_PLATFORM;
    registerRequest.mobileNumber = mobileNo;
    registerRequest.mobileCountryCode = mSelectedCountryCode;
    registerRequest.deviceInformation = Repository.DEVICE_INFO;

 Repository.apiService().register(registerRequest).enqueue(new BaseCallback<CommonResponse>() {
        @Override
        public void onSuccess(CommonResponse body) {
            super.onSuccess(body);
} 然后


您应该使用内部类和ArrayList。例如,如果您有:

{
    "A": 1,
    "B": {
        "C": "d"
    }
    "D": [
        1,
        2]
}
你将有:

public  class Model {
    private int A;
    private B B;
    private ArrayList<Integer> D;
    // Getters and setters

    public class B {
        private String C;
        // Getters and setters
}
公共类模型{
私人INTA;
私人B,;
私人ArrayList D;
//接球手和接球手
公共B级{
私有字符串C;
//接球手和接球手
}

您应该使用内部类和ArrayList。例如,如果您有:

{
    "A": 1,
    "B": {
        "C": "d"
    }
    "D": [
        1,
        2]
}
你将有:

public  class Model {
    private int A;
    private B B;
    private ArrayList<Integer> D;
    // Getters and setters

    public class B {
        private String C;
        // Getters and setters
}
公共类模型{
私人INTA;
私人B,;
私人ArrayList D;
//接球手和接球手
公共B级{
私有字符串C;
//接球手和接球手
}

您可以将下面的代码用作序列化模型类

@SerializedName("REG_CompanyAddress")
public CompanyAddress address = new CompanyAddress();

@SerializedName("REG_CompanyService")
public List<Service> services = new ArrayList<>();  

public static class Service {

    @SerializedName("ServiceID")
    public Integer serviceID;

}   

 public static class CompanyAddress {

    @SerializedName("AddressType")
    public String addressType;

    @SerializedName("Address1")
    public String address1;

    .......
    Your code 

    }
@SerializedName(“注册公司地址”)
公共公司地址=新公司地址();
@序列化名称(“注册公司服务”)
public List services=new ArrayList();
公共静态类服务{
@SerializedName(“ServiceID”)
公共整数serviceID;
}   
公共静态类公司地址{
@SerializedName(“地址类型”)
公共字符串地址类型;
@序列化名称(“地址1”)
公共字符串地址1;
.......
你的代码
}

像这样,您可以为其他字段创建

您可以使用下面的代码作为序列化模型类

@SerializedName("REG_CompanyAddress")
public CompanyAddress address = new CompanyAddress();

@SerializedName("REG_CompanyService")
public List<Service> services = new ArrayList<>();  

public static class Service {

    @SerializedName("ServiceID")
    public Integer serviceID;

}   

 public static class CompanyAddress {

    @SerializedName("AddressType")
    public String addressType;

    @SerializedName("Address1")
    public String address1;

    .......
    Your code 

    }
@SerializedName(“注册公司地址”)
公共公司地址=新公司地址();
@序列化名称(“注册公司服务”)
public List services=new ArrayList();
公共静态类服务{
@SerializedName(“ServiceID”)
公共整数serviceID;
}   
公共静态类公司地址{
@SerializedName(“地址类型”)
公共字符串地址类型;
@序列化名称(“地址1”)
公共字符串地址1;
.......
你的代码
}

与此类似,您可以为其他字段创建

手动定义POJO以进行JSON序列化是一项繁琐的任务,并且可以(至少部分)通过使用自动化。对于您的JSON,它会生成以下输出:

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("REG_Company")
@Expose
public REGCompany rEGCompany;
@SerializedName("REG_CompanyAddress")
@Expose
public REGCompanyAddress rEGCompanyAddress;
@SerializedName("REG_CompanyService")
@Expose
public List<REGCompanyService> rEGCompanyService = null;
@SerializedName("REG_CompanyHours")
@Expose
public List<REGCompanyHour> rEGCompanyHours = null;
@SerializedName("REG_Member")
@Expose
public REGMember rEGMember;

}
-----------------------------------com.example.REGCompany.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class REGCompany {

@SerializedName("ReferenceCode")
@Expose
public String referenceCode;
@SerializedName("CompanyType")
@Expose
public String companyType;
@SerializedName("CompanyName")
@Expose
public String companyName;
@SerializedName("CompanyEmailAddress")
@Expose
public String companyEmailAddress;
@SerializedName("CompanyTier")
@Expose
public String companyTier;
@SerializedName("ContactNumber")
@Expose
public String contactNumber;
@SerializedName("OilChangePerMonth")
@Expose
public String oilChangePerMonth;
@SerializedName("OilChangePercentage")
@Expose
public String oilChangePercentage;
@SerializedName("ShellHelixQty")
@Expose
public String shellHelixQty;
@SerializedName("Remarks")
@Expose
public String remarks;

}
-----------------------------------com.example.REGCompanyAddress.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class REGCompanyAddress {

@SerializedName("AddressType")
@Expose
public String addressType;
@SerializedName("Address1")
@Expose
public String address1;
@SerializedName("Address2")
@Expose
public String address2;
@SerializedName("StreetName")
@Expose
public String streetName;
@SerializedName("CompanyCity")
@Expose
public String companyCity;
@SerializedName("CompanyState")
@Expose
public String companyState;
@SerializedName("PostalCode")
@Expose
public String postalCode;
@SerializedName("Longitude")
@Expose
public String longitude;
@SerializedName("Latitude")
@Expose
public String latitude;

}
-----------------------------------com.example.REGCompanyHour.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class REGCompanyHour {

@SerializedName("WeekDay")
@Expose
public Integer weekDay;
@SerializedName("IsOpen")
@Expose
public Integer isOpen;
@SerializedName("StartTime")
@Expose
public Integer startTime;
@SerializedName("CloseTime")
@Expose
public Integer closeTime;

}
-----------------------------------com.example.REGCompanyService.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class REGCompanyService {

@SerializedName("ServiceID")
@Expose
public Integer serviceID;

}
-----------------------------------com.example.REGMember.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class REGMember {

@SerializedName("UserID")
@Expose
public String userID;
@SerializedName("UserPassword")
@Expose
public String userPassword;
@SerializedName("Salutation")
@Expose
public String salutation;
@SerializedName("FirstName")
@Expose
public String firstName;
@SerializedName("LastName")
@Expose
public String lastName;
@SerializedName("MobileNumber")
@Expose
public String mobileNumber;
@SerializedName("EmailAddress")
@Expose
public String emailAddress;
@SerializedName("Gender")
@Expose
public String gender;
@SerializedName("ContactPreference")
@Expose
public String contactPreference;
@SerializedName("RegistrationType")
@Expose
public String registrationType;
@SerializedName("RegistrationMode")
@Expose
public String registrationMode;

}
------------------------------------com.example.example.java-----------------------------------
包com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课范例{
@序列化名称(“注册公司”)
@暴露
上市公司;
@序列化名称(“注册公司地址”)
@暴露
公共注册公司地址注册公司地址;
@序列化名称(“注册公司服务”)
@暴露
公共列表rEGCompanyService=null;
@序列化名称(“注册公司小时”)
@暴露
公共列表rEGCompanyHours=null;
@序列化名称(“注册会员”)
@暴露
公共注册会员;
}
-----------------------------------com.example.REGCompany.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课公司{
@SerializedName(“引用代码”)
@暴露
公共字符串引用代码;
@SerializedName(“公司类型”)
@暴露
公共字符串公司类型;
@序列化名称(“公司名称”)
@暴露
公共字符串公司名称;
@序列化名称(“公司邮箱地址”)
@暴露
公共字符串companyEmailAddress;
@序列化名称(“公司名称”)
@暴露
公共字符串扩频器;
@SerializedName(“联系人号码”)
@暴露
公共字符串联系人号码;
@序列化名称(“换油月”)
@暴露
公共管柱换油每月;
@SerializedName(“OilChangePercentage”)
@暴露
公共字符串百分比;
@SerializedName(“ShellHelixQty”)
@暴露
公共字符串shellHelixQty;
@序列化名称(“备注”)
@暴露
公开评论;
}
-----------------------------------com.example.REGCompanyAddress.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类REGCompanyAddress{
@SerializedName(“地址类型”)
@暴露
公共字符串地址类型;
@序列化名称(“地址1”)
@暴露
公共字符串地址1;
@序列化名称(“地址2”)
@暴露
公共字符串地址2;
@序列化名称(“街道名称”)
@暴露
公共字符串streetName;
@序列化名称(“公司城市”)
@暴露
公共字符串公司;
@序列化名称(“公司状态”)
@暴露
公共字符串公司状态;
@序列化名称(“后代码”)
@暴露
公共字符串后代码;
@序列化名称(“经度”)
@暴露
公共字符串经度;
@序列化名称(“纬度”)
@暴露
公共字符串纬度;
}
-----------------------------------com.example.REGCompanyHour.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类注册公司小时{
@序列化名称(“工作日”)
@暴露
公众工作日;
@SerializedName(“IsOpen”)
@暴露
公共整数等参线;
@序列化名称(“开始时间”)
@暴露
公共整数起始时间;
@SerializedName(“关闭时间”)
@暴露
公共时间;
}
-----------------------------------com.example.REGCompanyService.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类REGCompanyService{
@SerializedName(“ServiceID”)
@暴露
公共整数serviceID;
}
-----------------------------------com.example.REGMember.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类注册会员{
@SerializedName(“用户ID”)
@暴露
公共字符串用户标识;
@SerializedName(“用户密码”)
@暴露
公共字符串用户密码;
@序列化名称(“称呼”)
@暴露
公共字符串称呼;
@SerializedName(“名字”)
@暴露
公共字符串名;
@SerializedName(“姓氏”)
@暴露
聚氨基甲酸酯