0

I am actually making a login app but I am unable to login. Here is my mainactivity.java what could be I doing wrong ?

  ppublic class MainActivity extends AppCompatActivity {
private EditText email, password;
private Button btn_login;
private ProgressBar loading;
private static String URL_LOGIN ="http://itsolutionsnepal.com/attendance/api/employeeLogin";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loading= findViewById(R.id.loading);
    email = findViewById(R.id.editTextUserEmail);
    password = findViewById(R.id.editTextPassword);
    btn_login = findViewById(R.id.buttonRegister);


    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mEmail = email.getText().toString().trim();
            String mPassword = password.getText().toString().trim();

            if (!mEmail.isEmpty() || !mPassword.isEmpty()) {
                Login(mEmail, mPassword);
            }else{
                email.setError("Please Enter Email");
                password.setError("Please Enter Password");

            }


        }
    });

}

private void Login(final String email, final String password) {
    loading.setVisibility(View.VISIBLE);
    btn_login.setVisibility(View.GONE);

    StringRequest stringRequest = new StringRequest(Request.Method.POST,URL_LOGIN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject object = jsonArray.getJSONObject(i);
                            String name = object.getString("_first_name").trim();
                            String email =  object.getString("_email").trim();

                            //  JSONObject jsnobject = new JSONObject(response);
//                            JSONArray jsonArray = jsnobject.getJSONArray("");
                           //JSONObject object = jsonArray.getJSONObject(i);


//                            JSONObject jsonResponse = new JSONObject(response);
//
//                            JSONArray jsonArray = jsonResponse.getJSONArray("");

                        //JSONObject jsonObject = new JSONObject(response);
                       // Log.d("JSON", jsonObject.toString());
                        //String success = jsonObject.getString("");
                       // JSONArray jsonArray = jsonObject.getJSONArray("");
                     //   JSONArray jsonArray = new JSONArray(response);
                       // JSONObject jsonObject = new JSONObject("");
                     //   String code = jsonObject.getString("");

  //                            if (success.equals("1"))
   //                            {

  //                                    String name = 
 object.getString("_first_name").trim();
//                                    String email = 
  object.getString("_email").trim();


 //                                    Toast.makeText(MainActivity.this, 
 "Success 
 Login \nYour name: "+name+"\nYour Email: "+email,
 //                                            Toast.LENGTH_LONG).show();
                            System.out.println("Value of response - " + 
response);

                            System.out.println("First Name : " + name);
                            System.out.println("Email : " + email);
                                Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                                intent.putExtra("name",name);
                                intent.putExtra("email", email);
                                startActivity(intent);
                            loading.setVisibility(View.GONE);




                            //}

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btn_login.setVisibility(View.VISIBLE);
                        Toast.makeText(MainActivity.this, "Error" +e.toString(),
                                Toast.LENGTH_SHORT).show();

                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, "Error" +error.toString(),
                            Toast.LENGTH_LONG).show();
                    loading.setVisibility(View.GONE);
                    btn_login.setVisibility(View.VISIBLE);

                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("_email", email);
            params.put("_password", password);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

Here is my json response enter image description here

Here is my php code:

enter image description here

Here is my error log:

enter image description here

I am developing this for my college project. What is wrong with my code?

Edit: This is the full screen shot from postman

enter image description here

15
  • 1
    Did you notice the JSON string exception in your error log screenshot "java.lang.String cannot be..." [end of image]. I guess that line should give you a hint. And please do not post text as image (your php code), copy/paste the relevant parts. Commented Nov 29, 2018 at 9:04
  • I don't know android code very well, so I could be wrong, but I don't see anywhere in the Java code where you send the username and password to the server in the login request. You can use your PHP debug tools to verify that, potentially. Commented Nov 29, 2018 at 9:04
  • BTW md5 is not a secure way to hash your passwords any more. A brute force attack can guess the password without too much difficulty. PHP provides special functionality for doing this task correctly. Commented Nov 29, 2018 at 9:06
  • @Lookaji That says java.lang.String cannot be converted to JSONArray Commented Nov 29, 2018 at 9:09
  • 2
    Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. Commented Nov 29, 2018 at 9:33

1 Answer 1

2

The keys in the volley request contradict with your POSTMAN screenshot. You are adding an underscore before the password and email, because of that you are getting a string response from your php code: Login credential wrong please try again!. I will advice you to add a status code to the response and parse according to the error

  @Override
        protected Map<String, String> getParams(){
            Map<String, String> params = new HashMap<>();
            params.put("email", email);
            params.put("password", password);
            return params;
        }

Also using MD5 is not secure enter image description here

Sign up to request clarification or add additional context in comments.

11 Comments

The error is showing because you are getting that string Login credential wrong please try again! and I have checked on studio and it worked fine when I removed underscore
So what should I change can you help me out?
@ManishThapa remove the underscores from the getParams method's keys and try calling the func directly Login("[email protected]", "password"); if it helped please mark the answer as correct
But I have too many user name and passwords in the backend .. i just dont want single username or password
And where should I call directly Login("[email protected]", "password");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.