AWS Identity TVM registration from android

199 views Asked by At

In Identity TVM registration, Instead of redirecting the user to Identity TVM register.jsp to register, can I directly get username and password of user through my application(Because user must register to use my application) and send them to Identity TVM registration to get registered. If yes, how to do it?

1

There are 1 answers

0
user2167299 On

I had the same problem. The answer I came up with is to send an HTTP Post request from within your app that replicates what happens on the registration form.

You will need to create a new layout that captures the username and password (I copied the file login_menu.xml, renamed it register_menu.xml and changed some of the widget ids)

In the original Login.java file I changed the onclick action for the Register button to redirect to a new Activity that I called Register.java

In Register.java I use the register_menu.xml as the layout file and when a person clicks the register button the following code is run (It gets run from within an AsyncTask):

        String registration_url = (PropertyLoader.getInstance().useSSL() ? "https://" : "http://") + PropertyLoader.getInstance().getTokenVendingMachineURL() + "/registeruser";

        URL url = new URL(registration_url);

        Map<String,Object> params = new LinkedHashMap<String,Object>();
        params.put("username", username);
        params.put("password", password);

        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String,Object> param : params.entrySet()) {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");


        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setInstanceFollowRedirects(false);

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setUseCaches (false);

        conn.getOutputStream().write(postDataBytes);
        conn.getOutputStream().flush();

        response = conn.getResponseCode();
        Log.d(DEBUG_TAG, "The response is: " + response);
        return response;

I picked up some of the code from other StackOverflow posts about how to generate an HTTP Post using Java (Java - sending HTTP parameters via POST method easily) and from the Android Dev site about network connection best practices (http://developer.android.com/training/basics/network-ops/connecting.html)

This should help you get started on your own implementation.