简体   繁体   English

比较textviews和JSONArray中的值

[英]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 in to an app by way of entering a username and password, this username and password has to match a username and password from the jsonarray which i've retrieved from a phpmyadmin database. 我想让用户通过输入用户名和密码登录到应用程序,此用户名和密码必须与从phpmyadmin数据库中检索到的jsonarray中的用户名和密码匹配。 The username and password have to be in the same row. 用户名和密码必须在同一行。

Here's the function I use to retrieve my jsonarray: 这是我用来检索jsonarray的函数:

private void getData(){

JSONArray json;
try{
    user = editText1.getText().toString();
    password = editText2.getText().toString();

    json = readJsonFromUrl("http://localhost/indextest.php?function=getdata");

       Thread.sleep(1000);
  } catch (Exception e) {
    Log.e("BACKGROUND_PROC", e.getMessage());
  }

} }

I just need to know how to search the jsonarray for the values that i retrieve from my textviews. 我只需要知道如何在jsonarray中搜索从textviews中检索到的值即可。

If possible I would like to retrieve a value that I can easily assign to an if statement like such: 如果可能的话,我想检索一个可以轻松分配给if语句的值,例如:

public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button1:

                if  ( editText1 != null && editText1.length() != 0 && editText2 != null && editText2.length() != 0){    
                    getData();
                    m_ProgressDialog = ProgressDialog.show(HomeScreen.this,    
                              "Please wait...", "Checking Details...", true);
                        m_ProgressDialog.setCancelable(true);

                      if ( /*username and password match*/){
                      Intent i = new Intent(this, Afterlog.class);
                      startActivity(i);     
                      }

                    else{
                        Toast.makeText(HomeScreen.this, "The username and password did not match any in our database...", Toast.LENGTH_SHORT).show();   
                    }
                }
                else {
                    Toast.makeText(HomeScreen.this, "Please enter a user name AND a password...", Toast.LENGTH_SHORT).show();
                }
                break;

}
}

Two things: 两件事情:

  • Take a look at GSON . 看一下GSON It is a Google Library for encoding objects into json and then decoding json into objects. 这是一个Google库,用于将对象编码为json,然后将json解码为对象。 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: 本质上,您可以定义一个与JSON中接收的数据具有相同结构的类,然后使用GSON读取JSON并创建一个包含适当字段的类的对象。您的代码将类似于以下内容:

First, define your class structure for the data you are sending as JSON: 首先,为要作为JSON发送的数据定义类结构:

public class LoginData {
    public String Username; //These identifiers must match the JSON structure
    public String HashedPassword;

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

Then, once you receive the JSON, you can parse the information like this: 然后,一旦收到JSON,就可以解析如下信息:

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 (ie hashed). 密码应始终以加密形式存储(即散列)。 When the user provides their password to log in, you encrypt the provided password and compare the encrypted versions (ie 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. 相反,如果他们正在监视您的网络流量,他们将看到哈希密码,并且不完全知道用于哈希密码的算法,他们将无法计算原始密码。

Your code needs to run in an asyncTask because it is performing a network request: 您的代码需要在asyncTask中运行,因为它正在执行网络请求:

Here is an example: 这是一个例子:

class LoginTask extends AsyncTask<String, String, JSONObject>
{
    protected JSONObject doInBackground(String... urls)
    {

        return readJsonFromUrl(urls[0]);
    }

    protected void onPostExecute(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 = new Intent(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: 您的按钮应负责运行该任务:

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button1:
          user = editText1.getText().toString();
          password = editText2.getText().toString();
          new LoginTask().execute()
        break;
    }
}

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/ http://www.coderzheaven.com/2012/04/22/create-simple-login-form-php-android-connect-php-android/

Thanks for all the help @meda :) 感谢所有的帮助@meda :)

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

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