[英]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: 两件事情:
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);
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.