0

I have problem with getting sessionID with JSON. JSON is always returning new sessionID and when I'm trying to add sessionID to request response is wrong SessionID, it should return opened sessionID if it's open or open new sessionID and always return the same sessionID while it's opened.

It's my request class:

public abstract class RequestSender<T> {

    private final String requestUrl;

    DefaultHttpClient httpclient;

    InputStream inputStream;

    public RequestSender(String methodName) {
        this.requestUrl = Settings.URL + methodName;
    }

    public void execute() throws IllegalStateException, IOException {
        httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(requestUrl);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        inputStream = entity.getContent();
    }

    private String convertResponseToString() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream, "UTF8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        inputStream.close();
        return sb.toString();
    }

    protected JSONObject parseToJSON() throws JSONException, IOException {
        return new JSONObject(convertResponseToString());

    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public abstract T getResults() throws JSONException, IOException;

}

If anybody can help me I will be thankfull :).

OcuS
  • 5,320
  • 3
  • 36
  • 45
Mathew1990
  • 327
  • 2
  • 17

2 Answers2

1

Problem solved, it was caused by making new DefaultHttpClient() every time. It should be created only once when calling a getSessionId method

iCanHasFay
  • 662
  • 5
  • 12
Mathew1990
  • 327
  • 2
  • 17
0

What I did was, on first successful authentication (or request-response exchange) with server, I sent session ID explicitly with the "Login Successul" json message.

My Android App saves this session ID in a singleton and each and every request that is made, accompanies the sessionid with it.

The server scripts use the session id to verify whether a session was open with authenticated user or not.

On the server side, I use this script to re-create session on the basis of the sessionid:

<?php
    if($_REQUEST['sessionid'])  //see if client sent a sessionid
        session_id($_REQUEST['sessionid']); //create session on the basis of retreived session id
    session_start(); // start session
?>
kishu27
  • 3,140
  • 2
  • 26
  • 41
  • I made the same: public class DataContainer extends Application { private String sessionId; private String userLogin; public String getSessionId() { return sessionId; } public void setSessionId(String sessionId) { this.sessionId = sessionId; } public String getUserLogin() { return userLogin; } public void setUserLogin(String userLogin) { this.userLogin = userLogin; } } but when I send next request with this sessionID there is another sessionID in cookies? So it could be something wrong with server aplication part? – Mathew1990 May 29 '12 at 14:51
  • I'm sorry I should have mentioned this as well. I did some tweaks on server side as well. I added the code – kishu27 May 29 '12 at 16:50
  • I think it could not be problem with serwer it is propably problem with android communication, maybe I should add some proeprties ?? – Mathew1990 May 30 '12 at 12:56
  • I couldn't find any such option on app side, so I resorted to specify these items on my own. I guess its simple too, you could try it – kishu27 May 31 '12 at 13:05