通過HTTP發送gzip壓縮的數據,我用的答案商定SO問題@Ahmed。你最好使用json字符串對象,然後使用gzip libray進行壓縮。
對於JSON有很多有用的教程。下面的鏈接是真正有用的
http://www.vogella.com/articles/AndroidJSON/article.html
在這裏你可以看到簡單的方式來寫JSON
public void writeJSON() {
JSONObject object = new JSONObject();
try {
object.put("name", "Jack Hack");
object.put("score", new Integer(200));
object.put("current", new Double(152.32));
object.put("nickname", "Hacker");
} catch (JSONException e) {
e.printStackTrace();
}
}
,並壓縮和解壓縮使用gzip我在這裏從答案添加一些示例代碼https://stackoverflow.com/a/6718707/931982
public static byte[] compress(String string) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
byte[] compressed = os.toByteArray();
os.close();
return compressed;
}
public static String decompress(byte[] compressed) throws IOException {
final int BUFFER_SIZE = 32;
ByteArrayInputStream is = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
StringBuilder string = new StringBuilder();
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = gis.read(data)) != -1) {
string.append(new String(data, 0, bytesRead));
}
gis.close();
is.close();
return string.toString();
}
壓縮它,例如用gzip – 2013-03-11 13:17:03