Because of the project requirements, the interface protocol is the transfer of XML data format, so it is necessary to encapsulate and parse the data sent and received in XML.
There are many ways to encapsulate or parse XML data on the Web, but you need to write your own processing logic, I also checked the Internet, I found that the XmlSerializer is also encapsulated a layer, directly to convert JSON to xml,xml into JSON, Feel more convenient (in fact, I was lazy), my project is basically a class, the property in the class is the need to send the XML attribute name, and then constructed this class, directly with Gson into JSON, and then use XmlSerializer to convert JSON to XML, Finally HTTP sent out on the line, the return of the results, the result string directly to the XmlSerializer into JSON, and then use Gson directly to convert the JSON value to the result class properties, it feels convenient to many
The jar used
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactid>gson</ artifactid>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
< Version>2.4</version>
</dependency>
Paste Code:
XML Request structure: <?xml version= "1.0" encoding= "GBK"?> <operation_in> <process_code>uuuuuuuu</process_c Ode> <app_id>22222222</app_id> <access_token>abababababba</access_token> <
Sign/> <verify_code/> <req_type/> <terminal_id/> <accept_seq/>
<req_time>20170213154229</req_time> <req_seq>123123123</req_seq> <content> <eccode>asdadad</eccode> <cycle>1231</cycle> </content> </operatio N_in> public interface Abstractrequest {String getorderid ();} public class XMLRequest implements
St {public String app_id = "22222222";
Public String Access_token = "Abababababba";
Public String sign = "";
Public String Verify_code = "";
Public String terminal_id = "";
Public String Req_seq;
Public String Req_time; Public XMLreqUest () {String seq = Dateutil.getdaysandtimeforserialkey (System.currenttimemillis ());
This.req_time = seq;
This.req_seq = seq + randomutils.getbyterandom (5);
@Override public String Getorderid () {return this.req_seq;
} public class Specificxmlrequest extends XMLRequest {public String Process_code = "uuuuuuuu";
Public String Req_type = "01";
Public String accept_seq = "";
public Jsonobject content;
Public specificxmlrequest (String eccode,string cycle) {Jsonobject jsonobject = new Jsonobject ();
Jsonobject.put ("Eccode", Eccode);
Jsonobject.put ("cycle", cycle);
This.content = Jsonobject;
} public String serialization (Abstractrequest abstractrequest) {try {Gson Gson = new Gson ();
Jsonobject object = Jsonobject.fromobject (Gson.tojson (abstractrequest));
Logger.info ("Serialization object:" + object.tostring ()); XmlserializeR XmlSerializer = new XmlSerializer ();
Set XML root domain name, set XML does not display field type Xmlserializer.setrootname ("operation_in");
Xmlserializer.settypehintsenabled (FALSE);
Logger.info ("serialization result" + object.tostring ());
Return Xmlserializer.write (object, "GBK");
}catch (Exception e) {logger.error ("serialization error" + Abstractrequest,e);
return null; }
}
XML result structure: <?xml version= "1.0" encoding= "GBK"?> <operation_out> <req_seq>2017030118415778780</ req_seq> <resp_seq>0</resp_seq> <resp_time>20170301184200 </resp_time> <resp onse> <resp_type>0</resp_type> <resp_code>0000</resp_code> <resp_des C> <!
[cdata[Success]]> </resp_desc> </response> <content> <resultlist> <gprsinfo> <user_id>925046300047634331</user_id> <usercount>0< ;/usercount> <max_value>0</max_value> <cumulate_value>0</cumulate_ value> </gprsinfo> <gprsinfo> <user_id>925046300047634345< /user_id> <usercount>10008</usercount> <max_value>82001920</max_va Lue&gT <cumulate_value>344</cumulate_value> </gprsinfo> </resultlist> </conten t> <emergency_status>0</emergency_status> </operation_out>/** * Response interface, define some to determine whether the interface successfully fetched interface
The method of the result is * * Public interface Abstractresponse {Boolean issuccess ();
String Getresultcode ();
String getresultmsg ();
String Getdealresult ();
String getdealmsg ();
void Parserresponse (String response); /** * Xmlresponse class, define the basic response class, properties are public some properties such as Resp_code,resp_desc and so on to return the structure according to the concrete interface * * Public class Xmlresponse Imple
ments abstractresponse {protected String resp_type;
protected String Resp_code;
protected String Resp_desc;
protected Jsonobject content;
@Override public boolean issuccess () {return "0000". Equals (Resp_code);
@Override public String Getresultcode () {return resp_code;
@Override public String getresultmsg () { return RESP_DESC;
@Override public String Getdealresult () {return content.tostring ();
@Override public String getdealmsg () {return resp_desc; }/** * Specificxmlresponse class, a class that corresponds to a specific interface, properties that are unique to this interface, such as Gprsinfo/public class Specificxmlresponse extends Xmlresp
Onse {protected Object gprsinfo; Assigning a specific property to a specific interface The following judgment is due to the fact that there is not necessarily more than one gprsinfo in the return result XmlSerializer will parse into a Jsonarray single time to parse into Jsonobject @Override publ IC void Parserresponse (String response) {/*if (content!= null && content.size () >0) {Object obj
ECT = Content.get ("Resultlist");
If (object instanceof jsonarray) {gprsinfo = new Jsonarray ();
Gprsinfo = object;
}else if (object instanceof jsonobject) {gprsinfo = new jsonobject ();
Gprsinfo = ((Jsonobject) object). Get ("Gprsinfo");
}else {gprsinfo = ""; }
}*/
if (content!= null && content.size () >0) {Gprsinfo =content.get ("resultlist");
if (gprsinfo instanceof Jsonarray) {Iterator iterator = ((Jsonarray) gprsinfo). iterator ();
while (Iterator.hasnext ()) {Jsonobject Jsonobject = (jsonobject) iterator.next ();
System.out.println (Jsonobject.tostring ()); }}else if (Gprsinfo instanceof jsonobject) {gprsinfo = ((jsonobject) gprsinfo). Get ("Gprsinfo")
;
System.out.println (Gprsinfo.tostring ());
}else {System.out.println (Gprsinfo.getclass ()); /** * XmlSerializer method for parsing HTTP returned XML counterpart generation Specificxmlresponse class object/public static void Xmlserialize
R (String xml) {XMLSerializer XMLSerializer = new XMLSerializer ();
Jsonobject json = (jsonobject) xmlserializer.read (xml.tostring ());//xmlserializer constructed directly into JSONSystem.out.println (Json.tostring ());
Remove the required data from the constructed JSON jsonobject result = new Jsonobject ();
Jsonobject resultresponse = (jsonobject) json.get ("response");
Result.put ("Resp_type", Resultresponse.getstring ("Resp_type"));
Result.put ("Resp_code", Resultresponse.getstring ("Resp_code"));
Result.put ("Resp_desc", Resultresponse.getstring ("Resp_desc"));
Jsonobject resultcontent = (jsonobject) json.get ("content");
Result.put ("Content", resultcontent);
Gson Gson = new Gson (); Use Gson to assign JSON data directly to the result class specificxmlresponse Specificxmlresponse = Gson.fromjson (result.tostring (), Specificxmlres
Ponse.class);
assigning Specificxmlresponse.parserresponse (XML) to a specific attribute;
System.out.println (Jiangsumobilegprspoolresponse.getdealresult ()); }