【发布时间】:2015-07-24 05:36:41
【问题描述】:
在我的 android 应用程序中,我想使用 gzip 压缩将压缩数据发送到 php 服务器,并从服务器接收响应并解压缩响应(如果它被压缩)。但是当我发送压缩数据时,服务器变为空,而不是我发送的数据,它返回错误消息。
这是我用于压缩的代码--
public static String compress(String strData) throws Exception
{
ByteArrayOutputStream obj = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(obj);
gzip.write(strData.getBytes("UTF-8"));
gzip.close();
String outStr = obj.toString("UTF-8");
return outStr;
}
这是用于解压的代码---
public static String decompress(String str) throws Exception
{
byte[] bytes1 = str.getBytes("UTF-8");
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes1));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
String outStr = "";
String line;
while ((line=bf.readLine())!=null) {
outStr += line;
}
return outStr;
}
我还在 httppost 请求中添加了一个标头
httpPost.addHeader("Accept-Encoding", "gzip, deflate");
如果我想在发送到服务器之前压缩数据并接收响应并解压缩它(如果它是压缩的),谁能告诉我该怎么做。
【问题讨论】:
-
我们看不到您发送到服务器的内容。我们看不到您的 php 脚本。这样的猜测太多了。