Search code examples
androidpythonsl4a

What do I need to get my phone app to connect to a https database?


I want to build an app that talks to a database. Analogy would be the youtube app for example. It connects to a secure host (requires login and password), and then returns to user results based on their query.

What I need the app to do is:

  • Get user Login information (preferrably do it in a secure way and not store it unencrypted on the users phone - This is I think easily solved with GetPassword so not worried about this)
  • Once user clicks 'login' Authenticate the user on my site (no clue at all how to even research how to do this. I know where the source code for the login page from regular browser is but not sure what to do)
  • How do I do queries based on who the user is? (for example their email, and other related data).

I would love some specific examples as well as general, and if there is a tutorial for a way to get the phone app talking to a server I would love that as well. Or any other related tutorial for that matter.


Solution

  • You can use HttpPost and HttpGet requests to communicate with a server. HttpGet sends parameters in the url (ex: http://mysite.com/index.html?username=admin&password=cleartext). That is obviously not the preferred method for secure information. The HttpPost sends the data in a packet which is encrypted using https. Here's an example of sending user entered data to a web page.

        EditText usernameText = (EditText)findViewById(R.id.username);
        EditText passwordText = (EditText)findViewById(R.id.password);
        String postParameters = "u=" + usernameText.getText() + "&p=" + passwordText.getText();
        try {
            DefaultHttpClient kccClient = new DefaultHttpClient();
    
            HttpPost postRequest = new HttpPost("http://www.mywebsite.com/login.php");
            HttpEntity postEntity = new StringEntity(postParameters);
            postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
            postRequest.setEntity(postEntity);
    
            HttpResponse postResponse = kccClient.execute(postRequest);
            HttpEntity postResponseEntity = postResponse.getEntity();
    
            responseText.setText(EntityUtils.toString(postResponseEntity));
        } catch(Exception e) {
            responseText.setText(e.getMessage());
        }
    

    To use a secure connection, just change the web url to https://www.mywebsite.com/login.php.