0

I'm using a PHP script to access data in my database.

However, data transfer might be expensive if your plan doesn't cover 3G access. So if there's no Wifi around, it would be good to know, how much my application is actually downloading/uploading/using.

Is there a way I can determine how much Bytes I'm uploading (post arguments) and then downloading (string output/response of the php script)?

Code in use:

//http post
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(arguments));
    HttpResponse httpresponse = httpclient.execute(httppost); 
    HttpEntity entity = httpresponse.getEntity();
    is = entity.getContent();
    Log.e("log_tag", "connection success ");
} catch(Exception e) {
    Log.e("log_tag", "Error in http connection "+e.toString());
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marco7757
  • 735
  • 9
  • 16

1 Answers1

1

Use getEntity().getContentLength() to get the HTTP message size, in HTTP request and response. And save the consumed property in SharedPreferences to recover it in another session.

The code will look like:

static long consumed = 0;

    //http post
    long consumedNow = 0;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(arguments));
        consumedNow += httppost.getEntity().getContentLength();
        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity entity = httpresponse.getEntity();
        is = entity.getContent();
        consumedNow += httpresponse.getEntity().getContentLength(); 
        Log.e("log_tag", "connection success ");
    } catch(Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());
    } finally {
        consumed += consumedNow;
        SharedPreferences sp = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong("consumed", value);
        editor.commit();
    }
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
  • Hm. I'm not using HttpURLConnection. I posted the code I'm using above. Any ideas? – Marco7757 Nov 08 '13 at 17:45
  • Thanks a lot. What exactly does consumed hold in the end? Number of bytes or something? – Marco7757 Nov 08 '13 at 18:17
  • Yes, bytes. Convert to readable format using: http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java And you can save the consumed data in SharedPreferences (to recover in diferent user sessions). – Italo Borssatto Nov 08 '13 at 18:23
  • Okay. But how can this be? I'm getting a string that's 688 characters long as a response. And this is only 2 bytes? Is this possible? – Marco7757 Nov 08 '13 at 18:38
  • I'm not used to HttpClient and didn't test the code, but probably your http server is not sending the Content-Length filled. So, try calling getContentLength after reading the content or get the content and increase the consumedNow variable while reading. – Italo Borssatto Nov 08 '13 at 19:00
  • Calling getContentLength after reading doesn't work. It still says 2. Is there another way? – Marco7757 Nov 08 '13 at 22:06