我是Android开发的初学者,我的应用要求用户在LoginActivity
中输入他们的电子邮件和密码才能登录。如果凭据没问题,则应用应该启动WorkActivity
。
这是PHP脚本。为了进行测试,我只是将$email
和$password
设置为静态值,而不是从数据库中获取它们。
<?php
$response = array ("success"=> 0, "responseMessage"=> "Incorrect email/password combination");
$email = "mwangicj";
$password = "asd";
if($_SERVER['REQUEST_METHOD']=='POST'){
$request = json_decode(file_get_contents('php://input'));
if(!strcmp($email,$request->email)&& !strcmp($password,$request->password)){
$response['success'] = 1;
$response['responseMessage'] = "Login successful ".$request->email;
}
}
echo json_encode($response);
?>
&#13;
我正在使用Volley库向PHP发送数据和从PHP接收数据。这是LoginActivity
package com.mwangicj.Lugayocontributor;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class LoginActivity extends AppCompatActivity {
EditText loginEmail, loginPassword;
TextView message;
Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginEmail = (EditText) findViewById(R.id.loginEmail);
loginPassword = (EditText) findViewById(R.id.loginPassword);
loginButton = (Button) findViewById(R.id.loginButton);
message = (TextView) findViewById(R.id.message);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (connected()){
message.setText("");
String email = loginEmail.getText().toString();
String password = loginPassword.getText().toString();
if (!email.isEmpty() && !password.isEmpty()){ //Check if fields are filled in
logInUser();
}else{
Toast.makeText(getApplicationContext(), "Supply both email and password!",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(), "No internet", Toast.LENGTH_SHORT).show();
}
}
});
}
//executing user's login request
private void logInUser() {
postJsonRequest("http://10.0.2.2:1234/app/json.php");
}
//Starting work activity
void goToWork(){
Intent i = new Intent(getApplicationContext(), WorkActivity.class);
startActivity(i);
finish();
}
//preparing user's login details
private void postJsonRequest(String url) {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
JSONObject params = new JSONObject();
try {
params.put("email", loginEmail.getText().toString());
params.put("password", loginPassword.getText().toString());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.POST, url, params, new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
try {
String success = response.getString("success");
String responseMessage = response.getString("responseMessage");
if (success == "0"){
message.setText(responseMessage);
}else{
Toast.makeText(getApplicationContext(), responseMessage, Toast.LENGTH_LONG).show();
goToWork();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
message.setText("Error: " + error.toString());
}
});
queue.add(jsonObjectRequest);
}
//Checking internet connection
boolean connected (){
ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if(nInfo!=null && nInfo.isConnected()){
return true;
}else{
return false;
}
}
}
&#13;
问题是无论电子邮件和密码组合是否正确或错误,WorkActivity都会启动。
我该如何解决这个问题?
答案 0 :(得分:2)
问题是==
运算符在比较对象时检查引用相等性。要测试两个字符串的值相等,您应该使用equals()
方法。
来自String.equals()
的{{3}}:
public boolean equals(Object anObject)
将此字符串与指定对象进行比较。如果,结果是真的 并且仅当参数不为null并且是String对象时 表示与此对象相同的字符序列。
更改以下内容:
if (success == "0"){
message.setText(responseMessage);
}
对此:
if ("0".equals(success)){
message.setText(responseMessage);
}
或强>
将success
声明为原始int
,而不是String
:
int success = response.getInt("success");
String responseMessage = response.getString("responseMessage");
if (success == 0){
message.setText(responseMessage);
}
在String
平等时查看documentation。