2

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

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() . "' ");
}
4

1 回答 1

1

使用它:从如何将 session_id 保存为 cookie 值并将其发送到 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();
于 2014-03-28T07:31:03.480 回答