Android DefaultHttpClient Session Check - android

I'm very new to Android programming and I have following doubt.
I use DefaultHttpClient for making Http calls to my server. It works fine.
Are there any way to check whether the session is still live on my DefaultHttpClient object?
Is that possible to do so?
Thanks in advance.

Related

How to use URL Connection instead of HTTPClient in ANDROID to send POST request to PHP?

I was trying to do this using HTTPClient since all the tutorials on the web are using HttpClient. But then I get the message Cannot resolve HTTPClient and that is proabably because it is deprecated since SDK 23. And there are not many tutorials to send POST request using URL Connection.
So can anyone please tell me how to do that ?
Basically let's just say, I ask user to enter the name and when he clicks submit button that is stored in PHP database.
P.S. : I know about ASynk Task, and I know Php and SQL.

disable/ignore cookies with httpclient

How can I disable/ignore default cookie handling of httpclient. I want to do it manually. I want to set a pre-defined cookie header for all http requests.
The latest httpclient (4.5.1) has a method called "disableCookieManagement", and it appears this just disables the internal cookie management, not the ability to send or receive cookies, and is working for me-
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#disableCookieManagement()
Set httpclient.setCookieStore(cookieStore); before you execute your HttpClient
Force to setup the HttpContext before executing the request:
private HttpClientContext httpContext = new HttpClientContext();
httpContext.setCookieStore(new BasicCookieStore());
The setCookieStore will create an empty cookie store to replace the default cookie store of http connection.
After that we can execute the http method:
org.apache.http.client.HttpClient.execute(HttpUriRequest, HttpContext)
Also we can reuse the cookie store (hold the BasicCookieStore) to keep the connection alive.
I could not find a way to disable/ignore cookie handling by DefaultHttpClient in Andoroid(I should have explored more into Android source code but have a time limitation). But I resolved it by removing all cookies before doing httpClient.execute() like this -
((AbstractHttpClient) myDefaultHttpClient).getCookieStore().clear();
This removes all the cookies stored by the defaultHttpClient and then you can manually handle(add/delete) cookies using -
myHttpPost.setHeader("Cookie", myCookie);
Hope it helps.

How to invoke a method in a session bean from an Android client?

I want to know whether it is possible to create an Android application to communicate with a session bean and invoke a method. if so can anybody explain how? or else can i invoke that method in the EJB with a JSP/servelet and call the JSP/Servelet with Android clients.. examples are highly appreciate
Thanks !!!
It is possible to communicate with Servelet in Android using HttpClient, HttpPost and HttpGet classes in android..
It is in theory relatively simple. Servlets can be configured by web.xml or #WebServlet annotation to get executed on a certain request URL. On a HTTP GET request the doGet() method will be executed. On a HTTP POST request, the doPost() method will be executed. The business logic which the servlet executes can depend/rely on the presence of HTTP request parameters and/or the request URI pathinfo.
All you need to do is to fire a HTTP request with the right URL and/or the right request parameters and/or the right pathinfo to let the servlet execute the desired job.
The basic Java API offers the java.net.URL and java.net.URLConnection for this. A simple HTTP GET request can be executed as follows:
InputStream response = new URL("http://example.com/servleturl?foo=bar&bar=foo").openStream();
// ...
Firing HTTP POST requests is a bit more complex. It can be done with java.net.URLConnection as outlined in this mini-tutorial, but Android also ships with Apache HttpComponents Client which allows firing and handling HTTP requests with less lines of code and more self-explaining code.
On http://androidsnippets.org you can find a lot of examples with HttpClient.

how do i do authentification on android

I need to authenticate to use a rest web service.
I make a
HttpClient httpclient = new DefaultHttpClient();
an there must be a way to use UsernamePasswordCredentials on that client.
Can somebody please point me to a relevant example?
I have found some source code uding the HttpClient class in the org.apache.commons.SOMETHING_HTTP, but that doesn't exist on Android.
Cheers
This may help you : http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

How to signin/login to a website programmatically?

I am new to android. I am creating an application in which user authentication is needed, but to authorize application user needs to login first. After that the data can be received. I just wanna know is there any procedure to login to a remote website if there is no login api available for that.
i am waiting for the reply.
Thanks,
Arun
May be you can talk to your website with a REST interface ? I dont know. but may be you can try to use kind of HttpClient objects to dialog with the website. You can use HttpGet to make a request via the GET_METHOD. There are other method to use. Well, if you seach a bit on the web about these objects/method you should see some examples. Hope its help you.
Use HTTPClient to do this, it gives you access to cookies and caching mechanisms, so it's all you need to implement this.

Categories

Resources