简体   繁体   English

如何为共享首选项的用户添加条件

[英]How to add conditions for a user in shared preference

I am working on a quiz app where there are many quizzes and the score user obtain for each quiz is stored in server. 我正在开发一个测验应用程序,其中有许多测验,每个测验获得的分数存储在服务器中。 Is there a way to add a condition so that the score particular user gets in a quiz is logged only on his first try and If the user appear for the same quiz second or more time, His score is not logged. 有没有一种添加条件的方法,以便特定用户仅在第一次测验中获得的分数就被记录下来;如果该用户第二次或更多次出现相同的测验,则不会记录其分数。

Following is the part of code where the score is sent to the server. 以下是将分数发送到服务器的代码部分。

 private void sendScoreToServer(String subcategory, String username, String email, String score){
    Map<String, String> params = getParams(subcategory, username, email, score);
    GsonRequest<ScoreObject> serverRequest = new GsonRequest<ScoreObject>(
            Request.Method.POST,
            Constants.PATH_TO_ADD_SCORE,
            ScoreObject.class,
            params,
            createRequestSuccessListener(),
            createRequestErrorListener());

    ((CustomApplication)getApplication()).getNetworkCall().callToRemoteServer(serverRequest);
}

private Map<String, String> getParams(String subcategory, String username, String email, String score){
    Map<String, String> params = new HashMap<String,String>();
    params.put(Constants.SUBCATEGORY, subcategory);
    params.put(Constants.NAME, username);
    params.put(Constants.EMAIL, email);
    params.put(Constants.SCORE, score);
    return params;
}

private Response.Listener<ScoreObject> createRequestSuccessListener() {
    return new Response.Listener<ScoreObject>() {
        @Override
        public void onResponse(ScoreObject response) {
            try {
                if(response != null){
                    Log.d(TAG, "Response value " + response);
                } else{
                    displayErrorMessage(ResultActivity.this, "Quiz score failed to upload");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}

private Response.ErrorListener createRequestErrorListener() {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    };
}
}

As for as I know It can be done in two ways, either by using shared preference or adding values to server. 据我所知,它可以通过两种方式来完成,即使用共享首选项或向服务器添加值。 Any input is welcomed. 欢迎任何输入。

I am a total newbie in coding so please bear with me. 我是编码方面的新手,请耐心等待。

Assuming you want them to be able to retake the quiz but not store the score again- the easiest and most secure way is on the server. 假设您希望他们能够重新参加测验但不再次存储分数-最简单,最安全的方法是在服务器上。 When the user uploads a score, check and see if the user already has a score for that quiz. 当用户上传分数时,请检查并查看用户是否已经有该测验的分数。 If he does, do not save the new score. 如果他这样做,请不要保存新分数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM