2017-07-26 58 views
0

我试图使用改进将这些数据发布到服务器。 所以我尝试创建序列化请求来发布这些数据。 通常如果像使用改进将数据发布到服务器android

{ 
"FirstName":"test", 
"LastName":"test", 
"EmailAddress":"[email protected]", 
"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; 

} 然后

@Headers({TOKEN, CONTENT_TYPE_JSON, LANGUAGE_CODE, APP_VERSION}) 
@POST("api/RegisterStep1") 
Call<CommonResponse> register(@Body RegisterRequest registerRequest); 

然后我张贴这样

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); 
数据

但现在我想知道如何创建序列化类张贴此以下格式的数据到服务器

{ 
"REG_Company": { 
    "ReferenceCode":"ReferenceCode", 
    "CompanyType":"1", 
    "CompanyName": "Test", 
    "CompanyEmailAddress": "[email protected]", 
    "CompanyTier": "1", 
    "ContactNumber": "99999", 
    "OilChangePerMonth": "1", 
    "OilChangePercentage": "1", 
    "ShellHelixQty": "1", 
    "Remarks": "1" 
}, 
"REG_CompanyAddress": { 
     "AddressType":"1", 
     "Address1":"Address1", 
     "Address2":"Address2", 
     "StreetName":"StreetName", 
     "CompanyCity":"CompanyCity", 
     "CompanyState":"CompanyState", 
     "PostalCode":"PostalCode", 
     "Longitude":"Longitude", 
      "Latitude":"Latitude" 
}, 
"REG_CompanyService": [ 
     {"ServiceID":1 
     },{"ServiceID":2}] 
     , 
"REG_CompanyHours": [ 
     {"WeekDay":1, 
     "IsOpen":1, 
     "StartTime":1, 
     "CloseTime":1 
     }, {"WeekDay":1, 
     "IsOpen":1, 
     "StartTime":1, 
     "CloseTime":1 
     }], 
"REG_Member": { 
    "UserID":"UserID", 
    "UserPassword":"UserPassword", 
    "Salutation": "1", 
    "FirstName": "FirstName", 
    "LastName": "LastName", 
    "MobileNumber": "MobileNumber", 
    "EmailAddress": "EmailAddress", 
    "Gender": "1", 
    "ContactPreference": "1", 
    "RegistrationType": "1", 
    "RegistrationMode": "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 

    } 

这样你CA创建对其他字段

0

您应该使用内部类的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 
} 
1

手动限定的POJO为JSON序列是一项乏味的任务,并且可以是(至少部分地)通过使用jsonschema2pojo自动化。对于您的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; 

} 

当然,您可以进一步重构生成的代码,例如,通过使主静态内部引用类。

相关问题