Android HttpGet request with a very long URL
I am currently faced to a strange problem.
I have to contact a web service, with a very long URL (there is some XML inside). The length of one of them is 943 characters. Most of the time, the request failed with a NoHttpResponseException. I newly added a RetryHandler, which do his job, and the request finally worked, but the execute time was 246 seconds! I reduced the timeout, to something like 30 seconds, and occasionally, the request work. Is there something to know about long URL to make it work better? Or, is it just prohibited on Android? I precise that all connection with another tinier URL (even like 200 chars) perfectly work.Here the source co开发者_Go百科de of the Http connection (partly based on this answer by kuester2000):
DefaultHttpClient hc = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(hc.getParams(), false);
HttpParams httpParameters = hc.getParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
hc.setParams(httpParameters);
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
// retry a max of x times
if(executionCount >= 5){
return false;
}
if(exception instanceof NoHttpResponseException){
return true;
} else if (exception instanceof ClientProtocolException){
return true;
}
return false;
}
};
hc.setHttpRequestRetryHandler(retryHandler);
url = Tool.prepareURL(url);
Log.d(LogFilter.EXECUTE, url);
HttpGet get = new HttpGet(url);
if (eTag != null) {
get.addHeader(HEADER_IF_NONE_MATCH, eTag);
}
long time = System.currentTimeMillis();
HttpResponse rp = hc.execute(get);
Log.d(LogFilter.EXECUTE, "temps execute: "+(System.currentTimeMillis()-time));
return rp;
Thank you for your time.
精彩评论