2

My requirement is very much similar to this question.

Basically, I will be having a login Activity in my android app and when the user enters data and clicks login, I have to hit my website, authenticates users, get the result back and guide the user further based on login success or not.

Here are my questions.

  1. what are the options for me to implement the above in android? How can I POST data and get results back in my Activity?
  2. If WebViews are used, can this be simplified?
1
  • Just adding to the other two answers: if you want to use WebView, it's going to be a plain web app. In the other case, be sure to use AsyncTask to avoid blocking UI thread.
    – bigstones
    Mar 2, 2011 at 17:19

3 Answers 3

4

You can POST to a URI with HttpClient:

URI uri = URI.create("http://whatever.com/thingie");
HttpPost post = new HttpPost(uri);
StringEntity ent = new StringEntity("Here is my data!");
post.setEntity(ent);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);

All the stuff you need to look at is in the package org.apache.http.client. There is a good amount of further examples out there on the internet to help you.

1

HttpClient is great for this. DroidFu, an opensource library has a fantastic example of how to efficiently use HttpClient. You can find it here.

1

Let me show using example code (sample code from one of my previous answer here on SO):

public CookieStore sendPostData(String url, String user, String pass) {

   // Setup a HTTP client, HttpPost (that contains data you wanna send) and
   // a HttpResponse that gonna catch a response.
   DefaultHttpClient postClient = new DefaultHttpClient();
   HttpPost httpPost = new HttpPost(url);
   HttpResponse response;

   try {   

      // Make a List. Increase the size as you wish.
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

      // Add your form name and a text that belongs to the actual form.
      nameValuePairs.add(new BasicNameValuePair("username_form", user));
      nameValuePairs.add(new BasicNameValuePair("password_form", pass));

      // Set the entity of your HttpPost.
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute your request against the given url and catch the response.
      response = postClient.execute(httpPost);

      // Status code 200 == successfully posted data.
      if(response.getStatusLine().getStatusCode() == 200) {
         // Green light. Catch your cookies from your HTTP response.
         CookieStore cookies = postClient.getCookieStore(); 
         return cookies;
      }
   } catch (Exception e) {
   }
}  

Now you need to set your cookies (or check/validate them) before doing requests against your server.

Sample code:

CookieStore cookieStore = sendPostData("www.mypage.com/login", "Username", 
                                            "Password");

// Note, you may get more than one cookie, therefore this list.
List<Cookie> cookie = cookieStore.getCookies();

// Grab the name of your cookie.
String cookieOne = cookie.get(0).getName();

What you really need to do is to check your HTTP response using an informative tool, such as Wireshark. Login via a computer browser and check/look for the correct values in your response (in Java/Android code you're using String value = cookie.get(0).getValue(); to get a value).

This is how you set cookies for your domain:

// Grab the domain of your cookie.
String cookieOneDomain = cookie.get(0).getDomain();

CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance(); 
cookieManager.setAcceptCookie(true);

cookieManager.setCookie(cookieOneDomain, cookieOne);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.