Skip to content Skip to sidebar Skip to footer

Comparing Values From Textviews And Jsonarray

Sorry for the (seemingly) lazy question, but i've been looking for a solution with no luck (in other words, I haven't found a solution that i understand). I want to have users log

Solution 1:

Two things:

  • Take a look at GSON. It is a Google Library for encoding objects into json and then decoding json into objects. In essence, you can define a class that has the same structure as the data you are receiving in JSON and then use GSON to read the JSON and create an object of your class with the appropriate fields filled in. Your code would look something like this:

First, define your class structure for the data you are sending as JSON:

publicclassLoginData{
    publicString Username; //These identifiers must match the JSON structurepublicString HashedPassword;

    public LoginData(String username, String hashedPass) {
        Username = username;
        HashedPassword = hashedPass;
    }
}

Then, once you receive the JSON, you can parse the information like this:

LoginData login = mGson.fromJson(json, LoginData.class);
  • It sounds like you are storing usernames and passwords in raw text and retrieving them from your database in raw text. This is a VERY BAD IDEA! Passwords should always be stored in an encrypted form (i.e. hashed). When the user provides their password to log in, you encrypt the provided password and compare the encrypted versions (i.e. compare the hash of the provided password to the stored hash from your database). This prevents people who might be listening to your network traffic from being able to capture your passwords. Instead, if they were watching your network traffic they would see the hashed password, and without knowing exactly the algorithm used to hash the passwords, they would not be able to calculate the original password.

Solution 2:

Your code needs to run in an asyncTask because it is performing a network request:

Here is an example:

classLoginTaskextendsAsyncTask<String, String, JSONObject>
{
    protectedJSONObjectdoInBackground(String... urls)
    {

        returnreadJsonFromUrl(urls[0]);
    }

    protectedvoidonPostExecute(JSONObject result)
    {

      try {
        //this assumes that the response looks like this://{"username" : "john" , "password" : "Bsd6578" }String responseUsername = result.getString("username");   
         String responsePassword = result.getString("password"); 

         if (user.equals(responseUsername) && password.equals(responsePassword)){
            Intent i = newIntent(this, Afterlog.class);
            startActivity(i);     
         }else{
            Log.d("mylog", "username and password dont match");
         }

       } catch (JSONException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
    }
}

Your button is should be responsible for running that task:

publicvoidonClick(View v) {
    switch (v.getId()) {
        case R.id.button1:
          user = editText1.getText().toString();
          password = editText2.getText().toString();
          newLoginTask().execute()
        break;
    }
}

Solution 3:

Found a very simple and to the point tutorial here:

http://www.coderzheaven.com/2012/04/22/create-simple-login-form-php-android-connect-php-android/

Thanks for all the help @meda :)

Post a Comment for "Comparing Values From Textviews And Jsonarray"