How to access a web content (POST/GET) in an Android application?
My requirement is very much similar to this question.
Basically, I will be having a login Activity in my andr开发者_Python百科oid 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.
- what are the options for me to implement the above in android? How can I POST data and get results back in my Activity?
- If WebViews are used, can this be simplified?
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.
HttpClient is great for this. DroidFu, an opensource library has a fantastic example of how to efficiently use HttpClient. You can find it here.
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);
精彩评论