【问题标题】:gzip compression in android app as client and php serverandroid 应用程序中的 gzip 压缩作为客户端和 php 服务器
【发布时间】: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 脚本。这样的猜测太多了。

标签: php android


【解决方案1】:

公共静态无效 main() { 字符串 myString = "H4sIAAAAAAAAAA5WMuwrCMBSGX0XOnCFpLoZ0ExH0FaTDyU0DTVsSB6H03Y3dHN3+67fCgm8wjAA6dz2DASmjUsjRCs8tahu5FCw6DQQmzKEt"; byte[] decode = Base64.decode(myString, Base64.NO_WRAP);

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decode);

    int read;
    try {
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);

        BufferedReader br = new BufferedReader(new InputStreamReader(gzipInputStream));
        System.out.println("read222 :" + br.readLine());


        DataInputStream dataInputStream = new DataInputStream(gzipInputStream);
        //System.out.println("read222 :" + dataInputStream.retoString());

        for (int i = 0; i < decode.length; i++) {
            read = dataInputStream.readInt();
            int rRead = Integer.reverseBytes(read);
            System.out.println("read :" + dataInputStream.read());

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

【讨论】:

    【解决方案2】:

    在线程或异步任务或服务中运行您的代码,无论您想要什么

    Thread thread=new Thread(new Runnable()
                        {
    
                            @Override
                            public void run()
                            {
                                try
                                {
                                    String body = "Naval is working on zip and unzip processed return json";
                                    URL url = new URL("http://yourServerUrl/yourgzip.php");
                                    URLConnection conn = url.openConnection();
                                    conn.setDoOutput(true);
                                    conn.setRequestProperty("Content-encoding", "gzip");
                                    conn.setRequestProperty("Content-type", "application/octet-stream");
                                    GZIPOutputStream dos = new GZIPOutputStream(conn.getOutputStream());
                                    dos.write(body.getBytes());
                                    dos.flush();
                                    dos.close();
                                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                                    StringBuilder builder=new StringBuilder(); 
    
                                    String decodedString = "";
                                    while ((decodedString = in.readLine()) != null)
                                    {
                                        builder.append(decodedString);
                                    }
                                    in.close();
                                    Log.e("Data", builder.toString());
    
                                }
                                catch (Exception e)
                                {
                                    Log.e("Datae", builder.toString());
                                }
    
    
                            }
                        });
                        thread.start();
    

    在服务器端要求他们使用此代码

    $postdata=gzinflate( substr($HTTP_RAW_POST_DATA,10,-8) );
    

    $HTTP_RAW_POST_DATA 已弃用,所以使用 file_get_contents() 来返回数组

    $post = array_map('urldecode',explode('&',file_get_contents("php://input")));
    

    或喜欢这种方法来解压推送到我的网络服务器的 gzip 流

    $x = file_get_contents("compress.zlib://php://input"); 
    

    并使用

    $data_as_associate_Array=json_decode($x,true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 2013-09-10
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      相关资源
      最近更新 更多