後端取出數據後發給前端,本來咱們使用json格式傳輸,每次傳輸將近1k左右,可是爲了給用戶省流量,咱們採起先把json去掉每個key,轉換爲二進制,而後gzip壓縮,這樣操做下來節省了70%的數據量,效果很好。
具體方法:
原有返回格式是這樣
<StoresList>
<return>
{
"status": {
"request_status": "0",
"error_code": "BTMes0100002",
"error_msg": "success"
},
"bizobj": [
{
"STORE_ID": 1,
"STORE_NAME": "肯德基",
"STORE_PHONE": "4008-823823",
"STORE_ADDRESS": "文二路肯德基店",
"STORE_LAT": "30.33331",
"STORE_LONG": "120.22221",
"PHOTO_TYPE": 2,
"RESOLUTION_TYPE": 2,
"PHOTO_URL": "http://www.kendeji.com/a.jpg"
}, {
"STORE_ID": 2,
"STORE_NAME": "麥當勞",
"STORE_PHONE": "15233336666",
"STORE_ADDRESS": "文一路麥當勞店",
"STORE_LAT": "30.33332",
"STORE_LONG": "120.22222",
"PHOTO_TYPE": 2,
"RESOLUTION_TYPE": 2,
"PHOTO_URL": "http://www.maidanglao.com/a.jpg"
} ],
"page_info": {
"cur_page": "1",
"page_size": "12",
"total_items": "3"
}
}
</return>
</StoresList>
咱們約定每個屬性的長度,在一個xml文件中寫成這樣
<storeList id="status">
<request_status>4</request_status>
<error_code>4</error_code>
<error_msg>40</error_msg>
</storeList>
<storeList id="page_info">
<cur_page>4</cur_page>
<page_size>4</page_size>
<total_items>4</total_items>
</storeList>
<storeList id="bizobj">
<STORE_ID>4</STORE_ID>
<STORE_NAME>50</STORE_NAME>
<STORE_PHONE>16</STORE_PHONE>
<STORE_ADDRESS>50</STORE_ADDRESS>
<STORE_LAT>12</STORE_LAT>
<STORE_LONG>12</STORE_LONG>
<PHOTO_TYPE>4</PHOTO_TYPE>
<RESOLUTION_TYPE>4</RESOLUTION_TYPE>
<PHOTO_URL>50</PHOTO_URL>
<CLASS_ID>4</CLASS_ID>
<STORE_URL>50</STORE_URL>
<distance>6</distance>
</storeList>
而後依次遍歷每一個storelist下的節點的ElementName以及ElementValue ,便可經過elementName獲得json中的值,經過elementValue來給json中的值轉換爲byte[],而且前補零。
最後把要拼裝的jsonobject或者jsonarray的byte數組拼裝成一個byte【】,而後對其gzip便可
public class BinaryUtil {
/**
* jsonobj轉換爲byte數組
* @param jsonObj
* @param charset
* @param xmlParameter
* @return
* @throws UnsupportedEncodingException
* @throws DocumentException
*/
public static byte[] convertJsonObj2ByteArray(JSONObject jsonObj, String charset,
XmlParameter xmlParameter) throws UnsupportedEncodingException,
DocumentException {
List<Jsonvalue> list = new ArrayList<Jsonvalue>();
List<XmlBean> xmllist = combineBizobj(xmlParameter);
for (XmlBean bean : xmllist) {
String name = jsonObj.getString(bean.getAttributeName());
Integer value = bean.getAttributeValue();
list.add(change2Jsonvalue(name, value, charset));
}
return combineList(list, charset);
}
/**
* jsonarray轉換爲byte數組
* @param jsonArray
* @param charset
* @param xmlParameter
* @return
* @throws DocumentException
* @throws UnsupportedEncodingException
*/
public static byte[] convertJsonArray2ByteArray(JSONArray jsonArray,
String charset, XmlParameter xmlParameter)
throws DocumentException, UnsupportedEncodingException {
if (jsonArray == null) {
return null;
}
int arraySize = jsonArray.size();
List<Jsonvalue> list = new ArrayList<Jsonvalue>();
List<XmlBean> xmllist = combineBizobj(xmlParameter);
for (int i = 0; i < arraySize; i++) {
for (XmlBean bean : xmllist) {
String name = jsonArray.getJSONObject(i).getString(
bean.getAttributeName());
Integer value = bean.getAttributeValue();
list.add(change2Jsonvalue(name, value, charset));
}
}
return combineList(list, charset);
}
/**
* 合併多個byte數組,合併後的長度爲多個子數組的和
* @param charset
* @param firstByteArray
* @param secondByteArray
* @param objects
* @return
* @throws UnsupportedEncodingException
*/
public static byte[] combineByteArray(String charset,byte[] firstByteArray,
byte[] secondByteArray, Object... objects) throws UnsupportedEncodingException {
if (firstByteArray == null) {
return secondByteArray;
} else if (secondByteArray == null) {
return firstByteArray;
} else {
StringBuffer sb=new StringBuffer();
sb.append(new String(firstByteArray));
sb.append(new String(secondByteArray));
if (objects != null) {
int j = objects.length;
for(int i=0;i<j;i++){
sb.append(new String((byte[])objects[i]));
}
}
return sb.toString().getBytes(charset);
}
}
/**
* 得到一個接口對應的全部的id
* @param xmlParameter
* @return
* @throws DocumentException
*/
public static List<String> getIds(XmlParameter xmlParameter)
throws DocumentException {
List<String> list = new ArrayList<String>();
List<Element> elist = getElementList(xmlParameter);
for (Element e : elist) {
list.add(e.attributeValue("id"));
}
return list;
}
/**
* gzip壓縮數據
* @param data
* @return
* @throws IOException
*/
public static byte[] gzipData(byte[] data) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzos = new GZIPOutputStream(bos);
gzos.write(data);
gzos.close();
return bos.toByteArray();
}
/**
* 得到接口名稱對應的配置元素列表
* @param method
* @return
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
private static List<Element> getElementList(XmlParameter xmlParameter)
throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlParameter.getXmlPath());
Element root = document.getRootElement();
List<Element> elist = root.selectNodes(xmlParameter.getElementName());
return elist;
}
private static byte[] combineList(List<Jsonvalue> list, String charset)
throws UnsupportedEncodingException {
StringBuffer result = new StringBuffer();
for (int i = 0; i < list.size(); i++) {
result.append(new String(list.get(i).getB()));
}
String f = result.toString();
return f.getBytes(charset);
}
/**
* 前補零
* @throws UnsupportedEncodingException
*/
private static byte[] fillZero(String charset,byte[] s, int bytesSize) throws UnsupportedEncodingException {
byte[] zreo = new byte[1];
zreo[0] = 0;
while (s.length < bytesSize) {
s = combineByteArray(charset,zreo, s);
}
return s;
}
private static Jsonvalue change2Jsonvalue(String value, Integer length,
String charset) throws UnsupportedEncodingException {
Jsonvalue jv = new Jsonvalue(fillZero(charset,value.getBytes(charset), length),
length);
return jv;
}
/**
* 得到一個element對應的全部節點
*
* @param xmlParameter
* @return
* @throws DocumentException
*/
@SuppressWarnings("unchecked")
private static List<XmlBean> combineBizobj(XmlParameter xmlParameter)
throws DocumentException {
List<XmlBean> list = new ArrayList<XmlBean>();
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlParameter.getXmlPath());
Element root = document.getRootElement();
List<Element> elist = root.selectNodes(xmlParameter.getElementName());
for (Element e : elist) {
if (e.attribute("id").getValue()
.equalsIgnoreCase(xmlParameter.getElementId())) {
List<Element> list1 = e.elements();
for (Element ee : list1) {
XmlBean xmlBean = new XmlBean(ee.getName(),
Integer.parseInt(ee.getData().toString()));
list.add(xmlBean);
}
}
}
return list;
}
}
壓縮包見附件包含自己的jar以及依賴的jar