-3

What I am trying to do is when user enters email and password I want to check it and based on success message want to print success, message and hash on Toast.maketext. My url for login is: https://www.digitalis.ba/korisnik2/android-getResult.php?a=login And for testing purpouses email should be equal to a@a.ba and pasword 12345, so when you change the link to: https://www.digitalis.ba/korisnik2/android-getResult.php?a=login&email=a@a.ba&pw=12345 you get success= 1.

So far I have this code and don't know where to register button, do I do this in onCreate or setOnClickListener or doInBackgroung

String data ="";
     String dataParsed = "";
    String singleParsed ="";
   EditText emailText = (EditText) findViewById(R.id.editText);
   EditText passwordText = (EditText) findViewById(R.id.editText2);
   String MAIN_URL = "https://www.digitalis.ba/korisnik2/android-getResult.php";
    String LOGIN_URL = "https://www.digitalis.ba/korisnik2/android-getResult.php?a=login";
    Button loginButton = (Button) findViewById(R.id.button);

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

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = emailText.getEditableText().toString().trim();
                String password = passwordText.getEditableText().toString().trim();


            }
        });
Kurt
  • 9
  • 5
  • Look at `Volley`: https://developer.android.com/training/volley/simple – Maxouille Mar 26 '19 at 15:20
  • @Maxouille Already tried, even tried number of tutorials, but it doesn't pass that a should be equal to login. – Kurt Mar 26 '19 at 15:23
  • Can you post your code ? What data are you collecting ? I don't understand clearly what's your trying to do. – Maxouille Mar 26 '19 at 15:24
  • @Maxouille if I put this url:"https://www.digitalis.ba/korisnik2/android-getResult.php" it is not valid, but this one "view-source:https://www.digitalis.ba/korisnik2/android-getResult.php?a=login&email=a@a.ba&12345" sends the success message, how can I make login that if email is equal to a@a.ba and pw =12345 programatically depended on some other credentials. Do you understand anything now xD ? – Kurt Mar 26 '19 at 15:41
  • Post the code you wrote. – Maxouille Mar 26 '19 at 15:44
  • @Maxouille I just posted edited version, can you please take a look ? Thanks! – Kurt Mar 27 '19 at 09:46
  • It looks to me like you simply need to parse the response from the server to your Android App. Using your links the data is being generated correctly. Have a look at this answer, it should provide you what you need: https://stackoverflow.com/a/33230067/8382028 – ViaTech Mar 27 '19 at 12:48

1 Answers1

1

The response you got from https://www.digitalis.ba/korisnik2/android-getResult.php?a=login&email=a@a.ba&pw=12345 is a JsonObject that looks like this :

{
  "success":1,
  "message":"",
  "hash":"askojgfiouw8ursaj"
}

From what I understand, you want to check if the user's login credentials are correct by calling the server at the address https://www.digitalis.ba/korisnik2/android-getResult.php?a=login and adding the data as parameters.

To do this, you need a HTTP POST request. That's how it should be done. A HTTP GET request works to and I'll show you that at the end of the post.

Using the Volley library, you can make HTTP requests easily. Below is the code of your onClickListener:

loginButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String email = emailText.getEditableText().toString().trim();
        String password = passwordText.getEditableText().toString().trim();
        
        // Create a JSONObject for adding parameters to the POST Request
        JSONObject postparams = new JSONObject();
        postparams.put("email", email);
        postparams.put("pw", password);
            
        // Creating the post request object
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, MAIN_URL, postparams,
            new Response.Listener() {
                @Override
                public void onResponse(JSONObject response) {
                    //Success Callback
                    successValue = response.getInt("success");
                    if (successValue == "1") {
                        Log.d("TAG", "Logged in succesfully");
                        // DO YOUR STUFF
                    }
                    else {
                        Log.d("TAG", "Login failed: Wrong email or password");
                        // DO YOUR OTHER STUFF
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //Failure Callback
                    Log.d("TAG", "Error posting request: "+error.toString());
                }
            });

        // Adding the request to the queue along with a unique string tag
        MyApplication.getInstance().addToRequestQueue(jsonObjectReq, "postRequest");       
    }
});

With a HTTP GET request, it works to but you have to concatenate your URL by yourself:

loginButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String email = emailText.getEditableText().toString().trim();
        String password = passwordText.getEditableText().toString().trim();
                
        LOGIN_URL = String.format("%s&email=%s&pw=%s", LOGIN_URL, email, password);
          
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, LOGIN_URL, null, new Response.Listener() {
            @Override
            public void onResponse(JSONObject response) {
                //Success Callback
                successValue = response.getInt("success");
                if (successValue == "1") {
                    Log.d("TAG", "Logged in succesfully");
                    // DO YOUR STUFF
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Failure Callback
                Log.d("TAG", "Login failed: Wrong email or password");
                // DO YOUR OTHER STUFF
            }
        });

        // Adding the request to the queue along with a unique string tag
        MyApplication.getInstance().addToRequestQueue(jsonObjectReq, "getRequest");
    }
});

Hope one of these solutions will work for you :)

Best

Community
  • 1
  • 1
Maxouille
  • 2,729
  • 2
  • 19
  • 42