mobile app Beginner
mobile app Beginner

Reputation: 1661

Is it possible to set timeout to disconnect established http connection?

I am writing a file upload app. I can connect to server and upload file (apache http) using asyn method.
The server has set the limit of number of connection to 100. I want to set a connection timeout to disconnect the connection if the connection last for 60s.

Here is the flow:
(1) Android client establishes the connection to server. (Start count for 60s)
(2) If the connection still alive after 60s, then disconnect from the Android client

I know that set timeout to HttpParams does not work. I have no idea of how to do it.
Anyone has idea of how to do it? Any solution or suggestion is welcome. Thanks!

Here is the code snippet of how to upload the file:

try
{
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 60000);
        HttpConnectionParams.setSoTimeout(httpParameters, 60000);


        HttpClient client =  new DefaultHttpClient(httpParameters);  
        ...
        ByteArrayBody bab = new ByteArrayBody(byteArray, "photo.jpg");
        entity.addPart("photo", bab);
        httpPost.setEntity(entity);   

        startTime = System.currentTimeMillis();


        //ClientConnectionRequest connRequest = new ManagedClientConnection();
        //httpPost.setConnectionRequest((ClientConnectionRequest) connRequest.getConnection(3000, TimeUnit.MILLISECONDS));

        HttpResponse response = client.execute(httpPost, localContext);

        Handler mHandler = new Handler();

        BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));  
        String result = "";  
        String tmp = "";



        while ((tmp = reader.readLine()) != null)
        {
            result += tmp;
        }
        reader.close();

        return result;

    } catch (Exception e) {  
        Log.e(TAG, "error time = " + (System.currentTimeMillis() - startTime));
        Log.e("httpUploadHandler", "Some error came up"); 
        Log.e("httpUploadHandler", e.toString()); 

    }   


I can disconnect the connection by calling:

client.getConnectionManager().shutdown();

But how can I count for 60s to disconnection the alive connection? Also, is it possible to know when the connection established?

Upvotes: 1

Views: 1234

Answers (2)

Akhil
Akhil

Reputation: 14038

Use a Handler in your Activity to do the job. Below code will give you an idea:

 private ShutDownHandler mRedrawHandler = new ShutDownHandler(); 

     class ShutDownHandler extends Handler {  
            @Override  
            public void handleMessage(Message msg) {  
         client.getConnectionManager().shutdown();
            }  

            public void doJob(long delayMillis) {  
              this.removeMessages(0);  
              sendMessageDelayed(obtainMessage(0), delayMillis);  
            }  
          };  

call mRedrawHandler.doJob(60000) from the main thread.

Upvotes: 1

Jason Robinson
Jason Robinson

Reputation: 31283

Have you tried setting the socket timeout? It differs from the connection timeout.

params.setParameter( HttpConnectionParams.SO_TIMEOUT, 60000 );

where params is an instance of BasicHttpParams, and it is passed into the constructor of DefaultHttpClient.

Upvotes: 2

Related Questions