Slow performance of uploading file using Httpurlconnection in android
I am trying to upload some user input information, text and an image
(converted into base64 string) to the server. I am using httpurlconnection
to accomplish this, but the performance is very slow. The image size is
600 X 600, and its taking about 119-125seconds to perform the upload,
which is a lot. Here is my code -
URL url_string = prepareURL(url_string, method);
url = new URL(url_string);
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setConnectTimeout(25000);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestMethod(method);
//method variable in the above line is just a string constant (GET or
POST)
if(method.equalsIgnoreCase(Constants.POST)) {
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
}
//toUseJson variable is us just a boolean to determine whether
//we have to set the chunking or fixedstreaming mode,
//based on GET method or POST method
if(toUseJson) {
//httpUrlConnection.setFixedLengthStreamingMode(json_message.getBytes().length);
//httpUrlConnection.setChunkedStreamingMode(1024*8);
httpUrlConnection.setChunkedStreamingMode(0);
}
httpUrlConnection.connect();
System.out.println("start - " + System.currentTimeMillis());
if(toUseJson) {
output_stream = httpUrlConnection.getOutputStream();
BufferedOutputStream buffered_output_stream = new
BufferedOutputStream(output_stream);
System.out.println("the bytes to read - " +
json_message.getBytes().length);
InputStream is = new
ByteArrayInputStream(json_message.getBytes("UTF-8"));
byte[] buffer = new byte[1024*1024];
int count = 0;
System.out.println("middle - " + System.currentTimeMillis());
while((count = is.read(buffer)) != -1) {
System.out.println("count - " + count);
buffered_output_stream.write(buffer, 0, count);
}
output_stream.flush();
}
........
I am trying various combinations to bring down the upload time, but, it
always takes around 110-120 seconds
Any help would be appreciated. Thanks!
No comments:
Post a Comment