The following code :
//
// Define HTTP Post and content
//
HttpPost httppost = new HttpPost(url);
ByteArrayEntity be = new ByteArrayEntity(strPostData.getBytes("UTF-8"));
httppost.setEntity(be);
//
// Define HTTP Client
//
HttpClient httpclient = new DefaultHttpClient();
HttpParams httpParameters = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10 * 1000);
//
// Sets the default socket timeout
// in milliseconds which is the timeout for waiting for data.
//
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpResponse response = httpclient.execute(httppost);
//
// This line takes too long on big responses
//
String content = EntityUtils.toString(response.getEntity());
The last line (EntityUtils.toString) takes too long when my response contains a large amount of Bytes.
I’m using HTTP Post to retrieve PDF files (up to 500 Kb) and it may take 1 or 2 seconds on each request, which is too much.
EDIT For Information : The PDF file is base 64 encoded and wrapped into XML Tag (the string is parsed after reception).
Is there any way to get my String response a faster way ?
EDIT 2 : in order to know how much time took my EntityUtils.toString, I made a method :
public static void logger(String header, String content) {
Date dateActualLog = new Date();
long milliseconds = (dateActualLog.getTime() - dateLastLog.getTime());
Log.d(header, String.valueOf(milliseconds) + " => " + content);
dateLastLog = dateActualLog;
}
(fyi : dateLastLog is a static variable)
I modified the above code like this :
//
// This line takes too long on big responses
//
logger(TAG, "toString ...");
String content = EntityUtils.toString(response.getEntity());
logger(TAG, "toString OK");
Thanks in advance.
Well, the first simple thing to try would be to ensure that your web-server is supplying a correct
ContentLengthheader in the HTTP response. Looking at some version of the source-code for HttpCore’sEntityUtilsclass, we see that if this information is not available, it defaults to using aCharArrayBufferof just 4k, buffering 1k of data when writing. On the 4th, 5th, and subsequent writes to theCharArrayBuffer(all the way up to 500, you say), it gradually increments the buffer by 1k … usingSystem.arrayCopy(). Yuck. There’s your performance misery right there.If speed is really important to you however, you’ll avoid using
EntityUtilsentirely. It’s just not responsible to turn a stream into a temporary 500kString… especially on a phone! You’ll need to find or write aBase64DecodingInputStreamorBase64DecodingReaderto wrap yourInputStreamfromresponse.getEntity().getContent(), and feed that … instead of aString… to your parser.