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:

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.