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 :).

4

2 回答 2

1

new DefaultHttpClient()问题解决了,是每次make造成的。getSessionId调用方法时应该只创建一次

于 2012-05-31T17:23:38.330 回答
0

我所做的是,在与服务器的第一次成功身份验证(或请求-响应交换)时,我使用“登录成功”json 消息显式发送会话 ID。

我的 Android 应用程序将此会话 ID 保存在一个单例中,并且发出的每个请求都伴随着会话 ID。

服务器脚本使用会话 ID 来验证会话是否已由经过身份验证的用户打开。

在服务器端,我使用这个脚本根据 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
?>
于 2012-05-29T14:33:56.547 回答