Sunday, August 1, 2010

[android-developers] HTTPClient hanging on occasion + really bizarre solution

I have a service that is loading images from a server. Almost every time the service runs it gets hung up on what seems to be misfire with the connection to the server. On closer inspection it looks as though HTTPClient spontaneously decides to hang as the last successful request from the server results in a status 200 and I don't see the next request go out. What's really bizarre is that it unblocks if I turn the device and force landscape (or vice-versa). If I'm loading, say 30 images, I'll have to turn the device portrait and landscape 2-3x for all the requests to complete sucessfully. Definately a wtf moment.

Here's my setup:

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion( params, HttpVersion.HTTP_1_1 );
            HttpProtocolParams.setContentCharset( params, HTTP.DEFAULT_CONTENT_CHARSET );
            HttpProtocolParams.setUseExpectContinue( params, true );
           
            HttpConnectionParams.setConnectionTimeout( params, Constants.HTTP_CONNECTION_TIMEOUT );
            HttpConnectionParams.setSoTimeout( params, Constants.HTTP_SOCKET_TIMEOUT );
            HttpConnectionParams.setStaleCheckingEnabled( params, true );
            HttpConnectionParams.setLinger( params, 0 );
           
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register( new Scheme( "http", PlainSocketFactory.getSocketFactory(), 80 ) );
            schemeRegistry.register( new Scheme( "https", SSLSocketFactory.getSocketFactory(), 443 ) );
           
            ClientConnectionManager connectionManager = new ThreadSafeClientConnManager( params, schemeRegistry );
           
            HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor()
            {
                public void process( final HttpRequest request, final HttpContext context ) throws HttpException, IOException
                {
                    AuthState authState = (AuthState) context.getAttribute( ClientContext.TARGET_AUTH_STATE );
                    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute( ClientContext.CREDS_PROVIDER );
                    HttpHost targetHost = (HttpHost) context.getAttribute( ExecutionContext.HTTP_TARGET_HOST );
                   
                    if( authState.getAuthScheme() == null )
                    {
                        AuthScope authScope = new AuthScope( targetHost.getHostName(), targetHost.getPort() );
                        Credentials creds = credsProvider.getCredentials( authScope );
                        if( creds != null )
                        {
                            authState.setAuthScheme( new BasicScheme() );
                            authState.setCredentials( creds );
                        }
                    }
                }   
            };
            httpClient = new DefaultHttpClient( connectionManager, params );
            httpClient.addRequestInterceptor( preemptiveAuth, 0 );


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment