How to reduce the size of a variable passed via POST by compressing it

Asked

Viewed 989 times

6

My application in Android picks up the String sends to the arquivo.php that processes the data.

I soon realized I couldn’t pass the code on base64 to the arquivo.php. I need some function in java what compact long String and send the same with a smaller size and that the arquivo.php can unzip it to its original state so I can manipulate the data.

Is there any way to do this? Reduce code by compressing it?

Follow the code I’m using.

public void postData(String html) {
    URL url = null;
    BufferedReader reader = null;
    StringBuilder stringBuilder;

    try {
        url = new URL("http://192.168.0.15/android.php");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
    conn.setDoOutput(true);

    Uri.Builder builder = new Uri.Builder()
            .appendQueryParameter("par", html);
    String query = builder.build().getEncodedQuery();

    OutputStream os = conn.getOutputStream();
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(os, "UTF-8"));

    writer.write(query);
    writer.flush();
    writer.close();
    os.close();


        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        stringBuilder = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            stringBuilder.append(line + "\n");
        }
        String output = stringBuilder.toString();
        Log.d("httpcliente", "BUSCANDO => [" + output + "]");
} catch (IOException e) {
    e.printStackTrace();
}


private class ParseURL extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        StringBuffer buffer = new StringBuffer();
        try {
            Log.d("JSwa", "Connecting to ["+strings[0]+"]");
            Document doc  = Jsoup.connect(strings[0]).get();
            Log.d("JSwa", "Connected to ["+strings[0]+"]");
            // Get document (HTML page) title
            String title = doc.title();
            Log.d("JSwA", "Title ["+title+"]");
            buffer.append("Title: " + title + "\r\n");

            // Get meta info
            Elements metaElems = doc.select("meta");
            buffer.append("META DATA\r\n");
            for (Element metaElem : metaElems) {
                String name = metaElem.attr("name");
                String content = metaElem.attr("content");
                buffer.append("name ["+name+"] - content ["+content+"] \r\n");
            }

            Elements topicList = doc.select("h2.topic");
            buffer.append("Topic list\r\n");
            for (Element topic : topicList) {
                String data = topic.text();

                buffer.append("Data [" + data + "] \r\n");
            }



            postData(doc.html());
        }
        catch(Throwable t) {
            t.printStackTrace();
        }

        return buffer.toString();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        respText.setText(s);
    }
}
  • I just need to send all the contents of the page to php so that he can process only the data that I desire, I did not find a way to achieve this in the Android/Java, pass multiple simultaneous requests containing the entire page would not generate a higher load? In my view yes, compacting the post would relieve more all this load. We are talking about more than 400 possible connections. Or would not need such a compression? Since the connection is fast, and the response also.

  • 1

    @Florida I try to create an example in java on android and send you

  • I tried not to use the StandardCharsets, tried in many ways, at first your code was not compatible with which the application is intended. 4.0 onwards.

1 answer

6


In accordance with @Luídne and the @bfavaretto Error 414 only occurs when data is passed through the URL (even if it is sends POST, yet you can send data through GET).

The mistake 414 Request-URI Too Long occurs when the provided URI has been too long to be processed by the server.

Source: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Sending POST to a Webservice

According to this reply on Soen, you can do it this way:

//Suas variáveis POST
String urlParameters  = "param1=a&param2=b&param3=c";

//Envia usando UTF8, altere conforme a necessidade
byte[] postData       = urlParameters.getBytes(StandardCharsets.UTF_8);

//Endereço do seu servidor
String request        = "http://example.com/index.php";

int    postDataLength = postData.length;
URL    url            = new URL(request);

HttpURLConnection conn= (HttpURLConnection) url.openConnection();           

conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );

//Necessário para o envio do POST
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");

conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
   wr.write( postData );
}

Compressing string

As this response from Soen, you can use the GZIPOutputStream to compress/compress the string:

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;
}

If you are using the method in the file MainActivity.java it will be necessary to import the necessary libraries, the start of Mainactivity should look something like:

package ...;

import java.lang.String;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPOutputStream;
import java.nio.charset.StandardCharsets;
import java.net.HttpURLConnection;

The use should be something like:

compress('Meu texto');

In PHP for you to unzip use gzuncompress (haven’t tested):

It should look something like:

echo gzuncompress($_POST['data'])

If it doesn’t work use gzdecode

  • Helped me a little, but I could not implement this function. I get the following message Required: java.lang.String and Found:byte[]. The code is running normally, I couldn’t compress the post, but it’s functional now. I upgraded it to HttpURLConnection for he wore HttpClient. I’ll keep looking for compatible alternatives.

  • I updated my comment. I added, but it did not help. Now I only have to sleep. At least with a cool head. Thanks for the personal help.

  • Without success, they are already included. This is the code I’m sweating, if you want to take a look http://pastebin.com/TQy4Lumz Note:My application uses version 4.0.1/4.1 on, so I noticed some things like the StandardCharsets may not work. I’ll come back later to try more.

  • I was using an outdated method to connect, I went to find out more and saw that I could have problems with it, I had to change. This code I found in Soen, I found this strange connect, but I had not used it before, I would never know that there were two, it seems that were wrong to post it. I will remove it, thanks for letting me know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.