我尝试使用json登录。我的php返回这个结果:
[{"name":"cynthia","password":"123456"},{"name":"John","password":"123456"}]
这些是mySQL中的数据。
我想用这个结果在android studio中进行比较,但我发现它没有用。它总是转到else语句。
public class LoginActivity extends AppCompatActivity {
public EditText username;
public EditText password;
private ArrayList<String> usernameList = new ArrayList<String>();
private ArrayList<String> passwordList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
}
public void Login2(View view) {
if (username.getText().length() == 0 || password.getText().length() == 0) {
openDialog(view);
} else {
new AsyncRetrieve().execute();
if(usernameList.contains(username)&&passwordList.contains(password)){
successLogin();
}else{
openDialog(view);
}
}
}
public void openDialog(View view) {
new AlertDialog.Builder(this)
.setTitle(R.string.login_dialog_title)
.setMessage(R.string.login_dialog_msg)
.show();
}
public void successLogin() {
Intent intent = new Intent();
intent.setClass(this, HomePageActivity.class);
startActivity(intent);
}
private class AsyncRetrieve extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
return getVideoList();
}
private String getVideoList() {
try {
URL url = new URL("http://localhost/soften/login_check1.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String response = reader.readLine();
//set the response from server as json array
JSONArray userArray = new JSONArray(response);
usernameList.clear();
passwordList.clear();
Log.d("username", "value = " + userArray.length());
for (int i = 0; i < userArray.length(); i++) {
Log.d("i", "i = " + i);
//get json object from the json array
JSONObject json = userArray.getJSONObject(i);
Log.d("json", "" + json.length());
//add object to the list for grid view
usernameList.add(json.getString("name"));
passwordList.add(json.getString("password"));
}
return null;
} catch (Exception ex) {
return ex.toString();
}
}
}
}
答案 0 :(得分:1)
你long2()是错误的,是吗
if(usernameList.contains(username)&&passwordList.contains(password))
你应该等于username.getText()和password.getText()。我的英语不是很好,但你应该能够理解
答案 1 :(得分:0)
问题出在这一行
new AsyncRetrieve().execute();
当您执行AsyncTask时,它会启动后台线程,但是您可以立即继续操作而无需等待来自服务器的结果,当您比较用户名和密码时,这些列表都是空的。
private ArrayList<String> usernameList = new ArrayList<String>();
private ArrayList<String> passwordList = new ArrayList<String>();
<强>解决方案:强>
第一个解决方案是向AsyncTask Call添加get()方法,如
new AsyncRetrieve().execute().get();
它将暂停您的AsyncTask,直到从服务器收到结果,然后进行比较。
另一个解决方案是覆盖AsyncTask中的onPostExecute()并在那里进行比较。
private class AsyncRetrieve extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
return getVideoList();
}
private String getVideoList() {
try {
URL url = new URL("http://localhost/soften/login_check1.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String response = reader.readLine();
//set the response from server as json array
JSONArray userArray = new JSONArray(response);
usernameList.clear();
passwordList.clear();
Log.d("username", "value = " + userArray.length());
for (int i = 0; i < userArray.length(); i++) {
Log.d("i", "i = " + i);
//get json object from the json array
JSONObject json = userArray.getJSONObject(i);
Log.d("json", "" + json.length());
//add object to the list for grid view
usernameList.add(json.getString("name"));
passwordList.add(json.getString("password"));
}
return null;
} catch (Exception ex) {
return ex.toString();
}
}
@Override
protected String onPostExecute(String result) {
if(usernameList.contains(username.getText())&&passwordList.contains(password.getText())){
successLogin();
}else{
openDialog(view);
}
}
}
从Login2方法中删除这些行:)不要忘记添加getText()方法。
希望得到这个帮助。