1

i am working in android and php. i called APIs which are coded in php.

I want to manage a session between this android application and php APIs.

means i want to protest a user who directly call my api by entering url of my API in address bar. whenever user loggedin in my android application then one session should be created on php side. so whenever call any APi then that session value should be checked first, then according to user verification, API should be allowed to valid loggedin user only.

I am using this line of code:- DefaultHttpClient httpClient = new DefaultHttpClient();

I dont have any idea how should do this. Please help me in this. You may provide me suitable link but please tell me related content only so my time wont waist.

Thank you in advance.

3 Answers 3

2

you can create a Singleton to DefaultHttpClient, then you use this to all your connections, before you send a HTTPRequest...

Like this:

public class SessionControl {
static private HttpClient httpclient = null;

public static HttpClient getHttpclient() {
    if( httpclient == null){
        SessionControl.setHttpclient(new DefaultHttpClient());
    }
    return httpclient;
}

public static void setHttpclient(HttpClient httpclient) {
    SessionControl.httpclient = httpclient;
}

}

then, when you call HttpClient, You do this:

HttpClient httpclient = SessionControl.getHttpclient();
Sign up to request clarification or add additional context in comments.

1 Comment

hello, I am using this singleton to DefaultHttpClient and using it in all connection.. But facing with some strange behavior. If for some time I don't send any HttpRequest,my connection seems to destroy and does not allow me further request. Can you please guide me where I am going wrong.
0

From your question it seems that you want to store the PHPSESSID between DefaultHttpClient calls.

You may try to use: HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);

when setting the params to your DefaultHttpClient instances. Otherwise you could manually get that session id and append it in every request.

Comments

-1

Well you might be creating some kind of session id on server side, which mobile application retrives and stores for futher use. Then everytime application connects to server it gives this id in parameter ( www.example.com?id=some_id ).

EDIT

Here is tutorial on connection to PHP server from Android device. It is not describing how to create ID, but as you can handle connection I am sure you will be able to create ID and store it in database link.

One more thing, to retrive parameter in given URL (www.example.com?id=some_id) in PHP you have to use $_REQUEST['id'] varible. If you want to send more parameters within given URL separete it with "&" sign (www.example.com?id=some_id&id2=some_id).

Be sure to check what is send in parameter, as someone might use your parameter for SQL injection purposes.

1 Comment

Yes sir, you are right... please give me one example for this. this will be very great help of you to me...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.