Hello, I am in some kind of a fix... I have been reading around the
subject above and I think Android materials available are very patchy,
and the Android documentation is not very useful without enough
experience. I have never worked with cookies in Java (only PHP)- and
the pages I have read don't discuss both initial AUTHENTICATION and
COOKIES together. And I am finding it difficult to bring together a
working program here. My idea is- I have a form that the user inputs
his credentials, on the click of the button the credentials are passed
to the server, the server responds with a cookie, and this is stored
for future use.
As per the piece of code below, is it possible to make the two
methods: authenticate() and xCookies() (receives, store and set
cookies with requests) work together?
If not please tell me how I can achieve the desired goal (preferably
using HTTP class vis-a-vis URL).
Your assistance will be highly appreciated.
.........................................................................................................
public class XClient extends Activity
{
EditText username;
EditText password;
TextView userTv;
TextView pwdTv;
@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.main);
login();
}
protected void login()
{
username = (EditText)findViewById(R.id.userNameLbl);
password = (EditText)findViewById(R.id.pwdText);
pwdTv = (EditText)findViewById(R.id.pwdLbl);
userTv =(TextView)findViewById(R.id.userNameText);
String userId = username.getText().toString();
String userPwd = password.getText().toString();
Button loginBtn = (Button)this.findViewById(R.id.authButton);
loginBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
authenticate(userId, userPwd);
}
});}
public void authenticate(String token1, String token2)//
Supposed
to handle authentication
{
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.useragent",
"My
Browser");
HostConfiguration host =
client.getHostConfiguration();
host.setHost(new URI("http://localhost:8080",
true));
Credentials credentials = new
UsernamePasswordCredentials
("token1", "token2");
AuthScope authScope =new AuthScope(host.getHost(),
host.getPort());
HttpState state = client.getState();
state.setCredentials(authScope, credentials);
GetMethod method = new GetMethod("www.gmail.com");
try{
client.executeMethod(host, method);
System.err.println(method.getStatusLine());
System.err.println(method.getResponseBodyAsString());
} catch(Exception e) {
System.err.println(e);
} finally {
method.releaseConnection();
}
}
//xCookies uses CookieManager class to store received cookie, and
sends the cookie with subsequent requests
private void xCookies()
{
CookieManager cm = new CookieManager();
try {
URL url = new URL("www.gmail.com");
URLConnection conn = url.openConnection();
conn.connect();
cm.storeCookies(conn);
System.out.println(cm);
cm.setCookies(url.openConnection());
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---