I can succesfully connect, send and receive data by using httpurlconnection. But it takes very long time to load all the data on my phone (Samsung s4, 4.2) and on android 4.2 emulator. But it takes almost 1-2 seconds (which is very fast) to load pics on Android 2.3.x emulator. Faster than my galaxy s4 on http connection.
I'm using AsyncTask and my code works fine on both. It is just slow on android 4.2s. I tried removing chunkedStreaming, keep alive, changing timeout values etc. but still no success
Here is my code
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setConnectTimeout(6000);
urlConnection.setReadTimeout(6000);
urlConnection.setRequestProperty("Content-Type", "multipart/form-data;charset=UTF-8;boundary="+boundary);
urlConnection.connect();
Are there any differences between 4.2's and 2.3.x's httpurlconnections? Whats wrong here
UPDATE!
I tested by using Log.e() to see which line takes most time.
///// other staff
////......
Log.e("HTTP","3");
if (isCancelled())
return (null); // don't forget to terminate this method
Log.e("HTTP","3");
//Output
DataOutputStream outputStream = new DataOutputStream( urlConnection.getOutputStream() );
//Send Passcode
Log.e("HTTP","4");
Between 3 and 4, 5-6 second passes on the line
DataOutputStream outputStream = new DataOutputStream( urlConnection.getOutputStream() );
UPDATE!!
That waiting time (see previous update) is related to the urlConnection.setConnectTimeout(6000);
When I make Timeout 1000, then connection responses quickly (waiting 1 second for the line)
DataOutputStream outputStream = new DataOutputStream( urlConnection.getOutputStream() );
No idea why this is happening
.connect()
, IgetOutputStream()
andgetInputStream()
right after setting the request parameters. – Covey