0

I am writing an app that will send an id, latitude, and longitude to a server using an HTTP POST. The app will be using user data. Is there a way to determine the size in bytes of the post before I send it?

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tvLatLong = (TextView) findViewById(R.id.tvLatLong);
    LocationServices.startUpdates(this, tvLatLong);

    Button btnPost = (Button) findViewById(R.id.btnSend);
    btnPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            id = 1;
            latitude = LocationServices.getLatitude();
            longitude = LocationServices.getLongitude();

            Log.i("USER ID ::", id + "");
            Log.i("USER LATITUDE ::", latitude + "");
            Log.i("USER LONGITUDE ::", longitude + "");

            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("keyId", id + ""));
            list.add(new BasicNameValuePair("latitude", latitude + ""));
            list.add(new BasicNameValuePair("longitude", longitude + ""));

            post(list);
        }
    });


}

private void post(List<NameValuePair> list) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://dmark3.apsu.edu");

    try{
        post.setEntity(new UrlEncodedFormEntity(list));


        HttpResponse response = client.execute(post);

        Log.i("HTTP RESPONSE :: ", response.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }


}

Above is the code.

Jordan Taylor
  • 167
  • 2
  • 11

1 Answers1

5

There's really no straight forward way to do measure the size of the http request sent to the server - primarily because the entire request is not assembled inside your program (which is where you will be calculating the size of the request). There will be other headers that get added on to your request that are part of the HTTP protocol. Also HTTPS complicates the process due to the certificates, etc..

There is an answer in here (Calculate HTTPS POST Request size) that talks about creating your own custom socket factory that calculates the size of the output stream and keeps a running total of the size but I'm not sure if you want to go that route.

Community
  • 1
  • 1
ucsunil
  • 7,378
  • 1
  • 27
  • 32