2

次のようにログインWebサービスを呼び出しています

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;
}

しかし、私は望ましい反応を得ていません。私のクッキーコードを見てください。私はどこで間違いを犯しましたか?

編集

私はこれを得る、

{"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

ここにセッションとクッキーの私の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 に答える