android - Keep connection between activities using DefaultHttpClient -
i'm developing app sends requests sap, i’ve been wrestling keep connection alive while i’m in app.
i got 3 activities: main, connection , list.
when start app goes main, after tap on connection establish connection between sap , device , connects successfully.
the issue have when press return button main , go list activity, cannot retrieve data because have lost defaulthttpclient had connection.
public string logintosap(string pip, int pport, string pschema, string puser, string ppassword) { string result = ""; // httphost targethost = new httphost(pip, pport, pschema); httphost targethost = new httphost(pip, pport, pschema); // defaulthttpclient httpclient = new defaulthttpclient(); httpclient.getcredentialsprovider().setcredentials(new authscope(targethost.gethostname(), targethost.getport()), new usernamepasswordcredentials(puser, ppassword)); // create authcache instance authcache authcache = new basicauthcache(); // generate basic scheme object , add local auth cache basicscheme basicauth = new basicscheme(); authcache.put(targethost, basicauth); // add authcache execution context basichttpcontext localcontext = new basichttpcontext(); localcontext.setattribute(clientcontext.auth_scheme_pref, authcache); httpget request = new httpget("/sap/z_conn"); responsehandler<string> handler = new basicresponsehandler(); try { result = httpclient.execute(targethost, request, handler); } catch (clientprotocolexception e) { e.printstacktrace(); toast.maketext(this, "wrong connection parameters", toast.length_short).show(); result = "wrong connection parameters"; } catch (ioexception e) { e.printstacktrace(); toast.maketext(this, "ioexception", toast.length_short).show(); result = "ioexception"; } return result; }
you extend application object , put defaulthttpclient in there.
how extend , use application object:
Comments
Post a Comment