0

I want to make a register/login app. I found a few examples but the most of this doesn't work for me. So I will do everything step by step. So first for the login I want to send the Server the username and the password of the user. So here is everything working, but as you can see I didn't send anything to the Server. So, where and how to send the "username" and "password" to the Server.

private void userLogin() {
    username = usernameField.getText().toString().trim();
    password = passwordField.getText().toString().trim();

    RequestQueue queue = Volley.newRequestQueue(this);

  // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast toast = Toast.makeText(getApplicationContext(), (response), Toast.LENGTH_SHORT);
                    toast.show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast toast = Toast.makeText(getApplicationContext(), "Anything doesn't work!", Toast.LENGTH_SHORT);
            toast.show();
        }
    });
 // Add the request to the RequestQueue.
    queue.add(stringRequest);
}
Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
mc.b
  • 93
  • 1
  • 4
  • 14
  • your request is GET or POST? – Amy Jan 24 '17 at 17:04
  • You say you "found a few examples but the most of this doesn't work for me" -- What examples? Are they using PHP + MySQL or Node + MongoDB or (web Framework X) and (database Y)? – OneCricketeer Jan 24 '17 at 17:18

3 Answers3

0

try the following got if your request is GET request.

private void userLogin() {
    username = usernameField.getText().toString().trim();
    password = passwordField.getText().toString().trim();

    RequestQueue queue = Volley.newRequestQueue(this);

  // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, LOGIN_URL+"?username="+username+"&password="+password, // here you have to add parameters of webservice
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast toast = Toast.makeText(getApplicationContext(), (response), Toast.LENGTH_SHORT);
                    toast.show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast toast = Toast.makeText(getApplicationContext(), "Anything doesn't work!", Toast.LENGTH_SHORT);
            toast.show();
        }
    });
 // Add the request to the RequestQueue.
    queue.add(stringRequest);
}
Amy
  • 4,034
  • 1
  • 20
  • 34
0

You can't send Data with a GET request HTTP body.

For that, you use a POST request.

Then, look at the other StringRequest constructors for the one that has more than 4 parameters for (method, url, onSuccess, onFailure).

OR... I assume you are using a JSON REST API, so you need a JSONObjectRequest

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Technically it is not forbidden to use a body in a get request.

It is however a very bad idea. (more info here: HTTP GET with request body)

This is why many http APIs do not allow you to do it.

Community
  • 1
  • 1
Hendrik Marx
  • 709
  • 4
  • 9