Not finding RequestParams with Rest service

193 views Asked by At

I'm writing an android application which uses rest services for user regitration and more but running into trouble with my login service. for some reason the requestparams i put into my service call on android side are not being found within my rest service. could anny 1 tell me what i'm doing wrong or link to a guide which explains how to solve this problem?

Relevant android functions:

public void loginUser(View view) {

    // Get username and password values
    String username = usernameEdit.getText().toString();
    String password = passwordEdit.getText().toString();

    // Instantiate Http Request Param Object
    RequestParams params = new RequestParams();

    // Check if username & password is not null
    if(Utility.isNotNull(username) && Utility.isNotNull(password)) {

        // Http parameters
        params.put("username", username);
        params.put("password", password);
        invokeWS(params);

    } else {
        Toast.makeText(getApplicationContext(), "Vul een gebruikersnaam en of " +
                "wachtwoord in", Toast.LENGTH_LONG).show();
    }
}

// Method that performs RESTful webservice invocations
public void invokeWS(RequestParams params) {

    // Make RESTful webservice call using AsyncHttpClient object
    AsyncHttpClient client = new AsyncHttpClient();

    client.post("http://10.0.2.2:8080/NTR_application/rest/session", params, new AsyncHttpResponseHandler() {

        // When the response returned by REST has Http response code '200'
        @Override
        public void onSuccess(String response) {
            Toast.makeText(getApplicationContext(), "You are successfully logged in!" + response, Toast.LENGTH_LONG).show();

            // Gets an JSON object with user Data
            // Write user Data to SQLite
            User user = new Gson().fromJson(response, User.class);
            db.addUser(user);

            // Navigate to Home screen
            navigatetoHomeActivity();
        }

        // When the response returned by REST has Http response code other than '200'
        @Override
        public void onFailure(int statusCode, Throwable error,
                              String content) {
            Toast.makeText(getApplicationContext(), "ERROR!" + content + error + statusCode, Toast.LENGTH_LONG).show();
        }
    });
}

and the rest services which is called :

@Path("/session")
public class UserService {

    private Controller controller = new Controller();

    @POST //Post so you can't see the information in the browser history easily
    @Produces(MediaType.APPLICATION_JSON)
    public Response authenticate(@QueryParam("username") String username, @QueryParam("password") String password){
        User user = null;
        try {
             user = controller.authenticate(username, password);
        } catch (NoSuchAlgorithmException | SQLException e) {
            System.out.println("Authentication caught an exception; failed for: " + username);
            e.printStackTrace();
        }
        if (user != null){
            String json = new Gson().toJson(user);
            return Response.status(200).entity(json).build();
        } else {
            return Response.status(401).entity("Username and/or password is incorrect").build();
        }
    }
}
1

There are 1 answers

0
Ralan On BEST ANSWER

Mistake was obvious once i saw it, since i use a @POST i need to use @FormParam instead of @QueryParam. tutorial i used to write these methods used @GET to login which is insecure.