输入正确登录凭据输出异常,传admin仍返回result:0问题求助
Got it, the issue you're facing is that your current login logic isn't properly recognizing when both the username and password are set to "admin" — that's why you're still getting {"result": 0} even with the correct credentials. Let's fix this step by step in your MainActivity code.
First, let's spot the common culprits
Most of the time, this problem comes from one of two mistakes:
- Using
==instead ofequals()to compare strings (Java checks object references with==, not the actual text content) - Writing a faulty condition that incorrectly triggers the error case even for valid credentials
Here's the corrected code example
Let's assume you're handling login input from EditText fields in your MainActivity. Replace your existing validation logic with this:
// Inside your MainActivity's login handler method public void onLoginButtonClick(View view) { // Grab input values from your UI elements EditText usernameInput = findViewById(R.id.et_username); EditText passwordInput = findViewById(R.id.et_password); String inputUsername = usernameInput.getText().toString().trim(); String inputPassword = passwordInput.getText().toString().trim(); // Validate credentials correctly if ("admin".equals(inputUsername) && "admin".equals(inputPassword)) { // Correct credentials: return your specified success JSON String successJson = "{\"result\": 1, \"message\": \"Login successful\", \"user_data\": {\"role\": \"administrator\"}}"; // You can display this, send it via an API response, etc. Toast.makeText(this, successJson, Toast.LENGTH_LONG).show(); } else { // Invalid credentials: return the error JSON String errorJson = "{\"result\": 0, \"message\": \"Invalid username or password\"}"; Toast.makeText(this, errorJson, Toast.LENGTH_LONG).show(); } }
Key fixes explained
- String comparison done right: Using
"admin".equals(inputUsername)(instead ofinputUsername.equals("admin")) also avoids a potentialNullPointerExceptionif the input is empty. - Strict condition: The
&&operator ensures both the username AND password must match "admin" to trigger the success case — no partial matches here. - Trimmed input:
.trim()removes accidental leading/trailing spaces from the user's input, which is a frequent hidden cause of validation failures.
If you're handling a network login endpoint in MainActivity
If your MainActivity is acting as a simple server (e.g., using NanoHTTPD) to handle login requests, here's how to adjust that logic:
// Example for a local login endpoint in MainActivity @Override public Response serve(IHTTPSession session) { Map<String, String> params = new HashMap<>(); try { session.parseBody(params); } catch (IOException | ResponseException e) { return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, "application/json", "{\"result\": 0, \"message\": \"Server error occurred\"}"); } String username = params.get("username"); String password = params.get("password"); if ("admin".equals(username) && "admin".equals(password)) { return newFixedLengthResponse(Response.Status.OK, "application/json", "{\"result\": 1, \"message\": \"Login success\", \"role\": \"admin\"}"); } else { return newFixedLengthResponse(Response.Status.UNAUTHORIZED, "application/json", "{\"result\": 0, \"message\": \"Invalid credentials\"}"); } }
Test this code with "admin"/"admin" and you should get the expected success JSON. For any other credentials, it'll return the error JSON as intended.
内容的提问来源于stack exchange,提问作者user5985713

