1

I am creating HttpClient as follow and using it for all my Posts and Gets methods.

HttpClient hc=createHttpClient();

public static HttpClient createClient() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,HTTP.DEFAULT_CONTENT_CHARSET);
    params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 36 * 1000);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 40 * 1000);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(conMgr, params);
}

I am using this httpclient as,,,

ApplicationClass ac=(ApplicationClass)getApplication(); ac.hc;

This client works perfect when app is open. But when i remove app from backstack i also want to use same httpclient when reopen . How to achieve this ?

Flow is like.:

App open -> User log in -> ...Some Operations... -> user closes app without logout

now if user reopens app then , here i should be able to connected with http client.. how to handle this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mind Android
  • 558
  • 2
  • 9
  • 27

1 Answers1

0

Keep your connection in a Service, perhaps run in the foreground if you don't want the system to stop it. But first of all make sure you actually need a permanent connection.

  • Ok, you just need to authenticate your requests. Store the authentication cookie or access token, so a new connection will be created on application start, but the user doesn't have to perform login again. http://stackoverflow.com/questions/678630/how-do-i-make-an-http-request-using-cookies-on-android http://stackoverflow.com/questions/8954569/how-to-put-user-authentication-into-a-mobile-application – Alexander Sukharev Mar 20 '14 at 10:33
  • Thanks .. but i can't understand how to work with HttpClient and CookieStore. is there any other way? – Mind Android Mar 22 '14 at 03:50
  • I am using this httpclient in all activities as,,, ApplicationClass ac=(ApplicationClass)getApplication(); ac.hc; – Mind Android Mar 22 '14 at 03:51