0

E.g. there are 10MB data stored in my tablet. The data has a list structure. Each entry in the list is about 3500 Bytes.

Currently, I send one entry each time with the following codes:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(ipport+ phpHandler);
    HttpResponse response = null;

    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
        response = httpclient.execute(httppost);
    } catch (UnsupportedEncodingException e) {
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

But to send this 10MB data, it took a long time. Each sending of an entry takes about 1 second.

Any solution to improve the efficiency?

JackWM
  • 10,085
  • 22
  • 65
  • 92

2 Answers2

1

You can build a JSON string object which contains all the entities and then compress it with gzip or any other compression scheme.

The benefit of building a JSON object is you can transmit all the objects as one request, instead of sending it separately. This would eliminate the latency of establishing a new connection everytime.

// your data list = listData
JSONArray newArray = new JSONArray();
for (int i = 0, lsize = listData.size(); i < lsize; i++) {
    try {
        newArray.put(i, listData.get(i));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

This code would build a JSONArray with all the elements in the listData (it should be a list of strings)

now you can easily convert the JSONArray to a string using

newArray.toString()

Now you can send this JSON string over the network, and you can easily deserialize a JSON object in any server side language.

As for Gzip compression, you might want to look at this link

Here is a question on SO about sending GZip compressed data over HTTP in android
GZip POST request with HTTPClient in Java

Community
  • 1
  • 1
Ahmed Aeon Axan
  • 2,139
  • 1
  • 17
  • 30
  • sounds interesting! Could you show some codes here? I'm not familiar with JSON and how to invoke gzip from Java. – JackWM Mar 11 '13 at 13:23
0

I am agreeing with the answer of @Ahmed. you better use jSON string object then compress using gzip libray.

for JSON there are lots of helpful tutorials. following link is really helpful

http://www.vogella.com/articles/AndroidJSON/article.html

here you can see the simple way to write 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();
  }
} 

and to compress and decompress using gzip Here i am adding some sample codes from the answer 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();
}
Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 1
    HTTP natively supports GZip of request/response bodies as part of its specification. Using HTTP client, the feature can be activated as is shown in the documentation examples at http://hc.apache.org/httpcomponents-client-ga/examples.html . Such a mecanism also allows for full body streaming which can reduce the memory footprint of the communication. – GPI Mar 11 '13 at 13:38
  • The native mecanism also handles properly String to byte conversion, following HTTP header contents and norms, which the given sample may fail to do (as `string.getBytes` and `new String(byte[])` will rely on default JVM encoding, whereas the conversion should be done according to HTTP header information). – GPI Mar 11 '13 at 13:49