通过httpget发送cookie值但未获得所需的响应

时间:2013-06-05 19:19:21

标签: android json http session session-cookies

我正在调用我的登录网络服务,如下所示

String url = "http://mydomaim.com/login.php";

        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.loginUser(userEmail, password, url);

它工作正常并发送给我如下所示的响应

{
 "userName":"a",
   "login_success":1,
   "user_id":"3",
   "session_id":"1067749aae85b0e6c5c5e697b61cd89d",
   "email":"a"
}

我解析了这个响应,并成功获得了会话ID,并将其存储在静态变量中(在MainActivity.java中),现在我必须将此会话ID作为cookie值发送到我的其他Web服务。

我这样做了

public JSONObject getJSONQuestion(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += "?" + paramString;

        CookieStore cookieStore = new BasicCookieStore();
        Cookie cookie = new BasicClientCookie("PHPSESSID",
                MainActivity.SESSION_ID);

                    // MainActivity.SESSION_ID has my session id

        cookieStore.addCookie(cookie);

        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet,
                localContext);

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}

但我没有得到理想的回应。请查看我的cookie代码。我犯了哪个错误?

修改

我明白了,

{"logout_success":1,"logout_reason":"no_activity_since_15_min"}

但我必须得到这个,

{


"random_questions":[
      {
         "questionid":"31",
         "question":"I hate V-",
         "answer1":"some answer 1",
         "answer2":"another answer 2",
         "answer3":"yet another answer 3",
         "answer4":"a great answer 4",
         "correct":"3"
      },
      … (more follow) …
   ]

  }

编辑2

这是我的会话和cookie的PHP代码

<?php

$min = 15 * 60;
//$min = 60;

if (time() - $_SESSION['LAST_ACTIVITY'] > $min) {
    session_destroy();
    setcookie("PHPSESSID", "", time() - 3600, "/"); // delete session cookie  
    echo json_encode(array('logout_success' => 1, 'logout_reason' => 'no_activity_since_15_min'));
    die;
} else {
    $_SESSION['LAST_ACTIVITY'] = time();
    mysqli_query($link, "UPDATE logged_in_users SET last_activity = " . $_SESSION['LAST_ACTIVITY'] . " WHERE session_id = '" . session_id() . "' ");
}

1 个答案:

答案 0 :(得分:1)

使用:来自How to save session_id as cookie value and send it to server in android

DefaultHttpClient httpClient = new DefaultHttpClient();
        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += "?" + paramString;
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);

        List<Cookie> cookies = httpClient.getCookieStore().getCookies();
        for (Cookie cookie : cookies) {
            System.out.println("Cookie: " + cookie.toString());

            if (cookie.getName().contains("PHPSESSID"))
                guid = cookie.getValue();

        }

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();