0

I am trying to send compress string message by android device to PHP server.

This is my android code

    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(string.getBytes());
    gos.close();
    byte[] compressed = os.toByteArray();
    os.close();

and this is my php code to decode

    $msg=$_GET['MsgType'];
    $msg2=urldecode($msg);
    print gzuncompress($msg2);

but there is error

Message: gzuncompress(): data error

i searched alot on google but didn't help if any one could help me most welcome.

1 Answers1

1

You should use a post variable because url's lenght is limited to few KBs...!

You should use the following code to compress the String

public static String compressAndSend(String str, String url) throws IOException {
    String body1 = str;
    URL url1 = new URL(url);
    URLConnection conn1 = url1.openConnection();
    conn1.setDoOutput(true);
    conn1.setRequestProperty("Content-encoding", "gzip");
    conn1.setRequestProperty("Content-type", "application/octet-stream");
    GZIPOutputStream dos1 = new GZIPOutputStream(conn1.getOutputStream());
    dos1.write(body1.getBytes());
    dos1.flush();
    dos1.close();
    BufferedReader in1 = new BufferedReader(new InputStreamReader(
            conn1.getInputStream()));
    String decodedString1 = "";
    while ((decodedString1 = in1.readLine()) != null) {
        Log.e("dump",decodedString1);
    }
    in1.close();
}

On PHP side use this,

<?php echo substr($HTTP_RAW_POST_DATA,10,-8); ?>

And please concern this Help manual for more information, http://php.net/manual/en/function.gzuncompress.php

Arsal Imam
  • 2,882
  • 2
  • 24
  • 35