in my app i try to get access to some web service. I'm going through form based authentication and getting an authorized connection. If i passed authorization i want open new Activity, but any new instance of DefaultHttpClient got unauthorized right.
And questions is:
1) How pass this connection through activity
2) Or how keep connection authorized correctly, if i think about httpclient interceptor, I'm on the right way? If, yes, then first question remain, but relativity of pass CookieStore data.
You should almost always make a class that handles your Http Requests call is ConnectionManager thats the most common name.
And you should make it with Singleton Design Pattern. This way your connections will be handled correctly.
public class ConnectionManager {
private static ConnectionManager instance = null;
private DefaultHttpClient client;
private ConnectionManager() {
client = new DefaultHttpClient(...);
}
//public method that will be invoked from other classes.
public static ConnectionManager getInstance() {
if(instance == null) {
instance = new ConnectionManager();
}
return instance;
}
public void authenticate(){
// Do your auth call with the client here
}
public void postStuff(){
// Use the same client here, this way you keep using the same client for ALL of your calls.
}
}
When you need to use the ConnectionManager use this:
private static ConnectionManager conn = ConnectionManager.getInstance();
conn.authenticate();
conn.postStuff();
Subclass the Application. You'll need to list it in the manifest so that it is created. You'll be able to retrieve it from within any activity.
public class ClientApplication extends Application {
Session Session = null;
public Session getSession() {
return Session;
}
public void setSession(Session session) {
Session = session;
}
}
elsewhere
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: Start");
super.onCreate(savedInstanceState);
setContentView(R.layout.startresume);
ClientApplication app = ((ClientApplication)getApplication());