Now I just started uploading my legs to HTTP. I used simple HTTP requests using GET and POST. The webpage I used is a 3-line php check for the correct $ _GET [] and $ _POST [], and then just the echo character "1". I use POST and GET with the same short pair of names / values, in the hope that packet fragmentation is not needed, and all this is done in a thread that is disconnected from the user interface thread. Requests are looped on the phone several times, synchronizing them. Everything works well. (See code below) That is, I return the answer "1". But there is a constant question of time. I observe that:
In the first attempt, the query execution time is much longer than subsequent attempts in both the GET and POST methods.
Other attempts are always much faster for both.
GET is always faster than POST.
All this is true for 3G and Wifi connections (but with Wi-Fi much faster, as expected).
I tried this with BasicResponseHandler () and with more manual buffered I / O methods with the same results.
I believe that I understand 3. as a result of the fact that POST requires two transfers, one for "HTTP 100" and then the body of the packet. - Is it correct?
My main question is what happens in the first request attempts, which are so slow? Sometimes it takes a few seconds (!). Is it a network that holds things or Android, putting it on a socket, creating some kind of queue? If it is Android, is there a way to code and avoid this more correctly? Is there something in keeping the socket open to make this problem only once at runtime? If so, is this good practice? Admittedly, this is apsect, which I don't know most about.
I found some discussion on this, but no one got into this aspect directly.
-
The basic code for the GET and POST methods looks like this (minus try / catch) and is executed in the thread from the user interface, first the GET method, then the POST method: (output below)
GET part of it:
public String[] HTTPGETIt(int numrounds) { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter (CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1); HttpGet GETRequest = new HttpGet("http://mypage.com/epoch.php?mynameis=tam"); ResponseHandler <String> MyBRH = new BasicResponseHandler(); String[] GETResult = new String[numrounds]; int i = 0; long timestart, DT; while(i < numrounds) { timestart = System.currentTimeMillis(); GETResult[i] = httpclient.execute(GETRequest, MyBRH); DT = System.currentTimeMillis() - timestart; Log.d(TAG, "(" + i + ") GET-Round Trip was "+ DT + " ms."); i++; }
And the POST version:
public String[] HTTPPOSTIt(int numrounds) { String Place = "HTTPPostping"; HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1); HttpPost PostRequest = new HttpPost("http://mypage.com/epoch.php"); ResponseHandler <String> MyBRH = new BasicResponseHandler(); String[] POSTResult = new String[numrounds]; List<NameValuePair> MynameValuePairs = new ArrayList<NameValuePair>(2); MynameValuePairs.add(new BasicNameValuePair("mynameis", "tam")); PostRequest.setEntity(new UrlEncodedFormEntity(MynameValuePairs)); int i = 0; long timestart, DT; while(i < numrounds) { timestart = System.currentTimeMillis(); POSTResult[i] = httpclient.execute(PostRequest, MyBRH); DT = System.currentTimeMillis() - timestart; Log.d(TAG, "(" + i + ") POST-Round Trip was "+ DT + " ms."); i++; }
They cause:
Runnable HTTPGETJob = new HTTPGETTask(NS); Thread HTTPGETThread = new Thread(HTTPGETJob, "HTTPGETThread"); HTTPGETThread.setPriority(Thread.MAX_PRIORITY); HTTPGETThread.start();
and
Runnable HTTPPOSTJob = new HTTPPOSTTask(NS); Thread HTTPPOSTThread = new Thread(HTTPPOSTJob, "HTTPPOSTThread"); HTTPPOSTThread.setPriority(Thread.MAX_PRIORITY); HTTPPOSTThread.start();
With runnables:
class HTTPGETTask implements Runnable { int numtimes; DeviceInfo tsrtDI; HTTPGETTask(int inNS) { this.numtimes = inNS; } @Override public void run() { long [] TT2NS = new long[numtimes]; TT2NS = HTTPGETIt(numtimes); } };
and
class HTTPPOSTTask implements Runnable { int numtimes; DeviceInfo tsrtDI; HTTPPOSTTask(int inNS) { this.numtimes = inNS; } @Override public void run() { long [] TT2NS = new long[numtimes]; TT2NS = HTTPPOSTIt(numtimes); } };
The output is usually:
(0) GET-Round Trip was 368 ms.
(1) The GET-Round Trip was 103 ms.
(2) The GET-Round Trip was 98 ms.
(3) The GET-Round Trip was 106 ms.
(4) The GET-Round Trip was 102 ms.
(0) POST-Round Trip was 1289 ms.
(1) POST-Round Trip was 567 ms.
(2) POST-Round Trip was 589 ms.
(3) POST-Round Trip was 496 ms.
(4) POST-Round Trip was 557 ms.