有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java向安卓项目添加score元素

我说我会展示下面三节课,这样你就能确切地知道我在做什么。我发布的三个类用于我正在开发的一个游戏,它们与应用程序的登录和注册部分有关。我想添加一个分数元素,当用户单击按钮并且满足某些条件时,该元素将增加。我只是想知道是否有人能帮我添加score元素(当用户单击按钮时,它将发布,您可以检索它)

服务器请求。爪哇

public class ServerRequests {
    ProgressDialog progressDialog;
    public static final int CONNECTION_TIMEOUT = 15000;
    public static final String SERVER_ADDRESS = "http://maynoothflag.netau.net/";

    //Progress box that you can't cancel, It works while you're logging in/registering etc.
    public ServerRequests(Context context)
    {
        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setTitle("Processing");
        progressDialog.setMessage("Please wait..");

    }
    //Store the data on the server in the background
    public void storeDataInBackground(Contact contact , GetUserCallback callback)
    {
        //when it is storing data in the background, our progress dialog will be shown
        progressDialog.show();
        new StoreDataAsyncTask(contact, callback).execute();
    }

    //When the user is registering, it will call this method to post the information to the server
    public void fetchDataInBackground(Contact contact , GetUserCallback callback)
    {
        progressDialog.show();
        new FetchDataAsyncTask(contact, callback).execute();
    }


    public class StoreDataAsyncTask extends AsyncTask<Void , Void , Void>
    {
        Contact contact;
        GetUserCallback callback;

        //Construcor for this new class
        public StoreDataAsyncTask(Contact contact , GetUserCallback callback)
        {
            this.contact = contact;
            this.callback = callback;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            ArrayList<NameValuePair> data_to_send = new ArrayList<>();

            data_to_send.add(new BasicNameValuePair("Name" , contact.name));
            data_to_send.add(new BasicNameValuePair("Email" , contact.email));
            data_to_send.add(new BasicNameValuePair("Username" , contact.username));
            data_to_send.add(new BasicNameValuePair("Password" , contact.password));

            HttpParams httpRequestParams = new BasicHttpParams();
            //Time to wait before process is executed
            HttpConnectionParams.setConnectionTimeout(httpRequestParams , CONNECTION_TIMEOUT);
            //Time we want to wait to recieve something from the server
            HttpConnectionParams.setSoTimeout(httpRequestParams , CONNECTION_TIMEOUT);

            //Create a new client
            HttpClient client = new DefaultHttpClient(httpRequestParams);
            //Create a new post, that will use or Register.php file (That is stored on our database)
            //This adds the information that the user entered in the Reigster.java file to our database
            HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

            try {
                //posts the information
                post.setEntity(new UrlEncodedFormEntity(data_to_send));
                client.execute(post);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            return null;
        }

        //after our task is done, we have to dismiss the progress dialog
        @Override
        protected void onPostExecute(Void aVoid) {
            progressDialog.dismiss();
            callback.done(null);

            super.onPostExecute(aVoid);
        }
    }

    //Fetches users data from the database
    public class FetchDataAsyncTask extends AsyncTask<Void , Void , Contact>
    {
        Contact contact;
        GetUserCallback callback;

        //Constructor for this class
        public FetchDataAsyncTask(Contact contact , GetUserCallback callback)
        {
            this.contact = contact;
            this.callback = callback;
        }


        @Override
        protected Contact doInBackground(Void... voids) {
            ArrayList<NameValuePair> data_to_send = new ArrayList<>();
            //It will only fetch the username and password, not the email (from the server)
            data_to_send.add(new BasicNameValuePair("Username" , contact.username));
            data_to_send.add(new BasicNameValuePair("Password" , contact.password));

            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams , CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpRequestParams , CONNECTION_TIMEOUT);

            HttpClient client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");

            Contact retunedContact = null;
            try {
                post.setEntity(new UrlEncodedFormEntity(data_to_send));
                HttpResponse httpResponse = client.execute(post);

                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);


                JSONObject jsonObject = new JSONObject(result);
                retunedContact = null;

                if(jsonObject.length() == 0)
                {
                    retunedContact = null;

                }
                else
                {
                    String name,email;
                    name = null;
                    email=null;

                    if(jsonObject.has("name"))
                        name = jsonObject.getString("name");
                    if(jsonObject.has("email"))
                        email =jsonObject.getString("email");

                    retunedContact = new Contact(name , email , contact.username , contact.password);

                }

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            return retunedContact;
        }
        @Override
        protected void onPostExecute(Contact returnedContact) {
            progressDialog.dismiss();
            callback.done(returnedContact);
            super.onPostExecute(returnedContact);
        }

    }
}

本地数据库。爪哇

public class LocalDatabase {
    public static final String SP_NAME = "UserDetails";
    //Shared Preferences allows us to store data on the phone
    SharedPreferences localDatabase;

    //A constructor for this class
    public LocalDatabase(Context context)
    {
        localDatabase = context.getSharedPreferences(SP_NAME , 0);
    }

    //Storing the data
    public void storeData(Contact contact)
    {
        //Allows us to edit the local database
        SharedPreferences.Editor spEditor = localDatabase.edit();
        spEditor.putString("Name" , contact.name);
        spEditor.putString("Email" , contact.email);
        spEditor.putString("Username" , contact.username);
        spEditor.putString("Password" , contact.password);
        //Saves the work
        spEditor.commit();
    }

    //This is for getting the data for which user is current logged in
    public Contact getLoggedInUser()
    {
        String name = localDatabase.getString("Name" , "");
        String email = localDatabase.getString("Email" , "");
        String username = localDatabase.getString("Username" , "");
        String password = localDatabase.getString("Password", "");


        //Parse your 4 variables
        Contact storedContact = new Contact(name , email , username , password);
        return storedContact;
    }

    //Tell us if the user is currently logged in or not.. THat's why we use a boolean
    public void setUserLoggedIn(boolean loggedIn)
    {
        SharedPreferences.Editor spEditor = localDatabase.edit();
        spEditor.putBoolean("loggedIn" , loggedIn);
        //Save all the work
        spEditor.commit();
    }

    //
    public boolean getUserLoggedIn()
    {
        if(localDatabase.getBoolean("loggedIn" , false))
            return true;
        else
            return false;
    }

    //When the user loggs out, it will clear all the temporary data off the phone of the currently logged in user
    public void clearData()
    {
        //Clear all the data and save our work
        SharedPreferences.Editor spEditor = localDatabase.edit();
        spEditor.clear();
        spEditor.commit();
    }

}

联系方式。爪哇

public class Contact {

    //Since the users id is auto incrementing in our database, we don't need to mention it here
    //This name, email, username and password will store the value of the below name, email, username and password
    String name , email , username , password;
    int score;

    //A constructor for signing up
    public Contact(String name , String email , String username , String password)
    {
        this.name = name;
        this.email = email ;
        this.username = username;
        this.password = password;
    }

    //A constructor for logging in
    public Contact(String username , String password)
    {
        this.username = username;
        this.password = password;
    }

    public Contact(String username , int score)
    {
        this.username = username;
        this.score = score;
    }

}

任何帮助都会很感激,也很抱歉发布这么大的代码块,我只是认为这会帮助人们了解代码。几天前我曾问过这个问题,但没有得到答案,所以我说我会对这个问题稍加改进,然后再次发布:)


共 (0) 个答案