2

在 Identity TVM 注册中,我可以直接通过我的应用程序获取用户的用户名和密码(因为用户必须注册才能使用我的应用程序),而不是将用户重定向到Identity TVM register.jsp进行注册,然后将它们发送到 Identity TVM 注册以获取挂号的。如果是,该怎么做?

4

1 回答 1

1

我有同样的问题。我想出的答案是从您的应用程序中发送一个 HTTP Post 请求,该请求复制注册表单上发生的情况。

您将需要创建一个捕获用户名和密码的新布局(我复制了文件 login_menu.xml,将其重命名为 register_menu.xml 并更改了一些小部件 ID)

在原始 Login.java 文件中,我更改了 Register 按钮的 onclick 操作,以重定向到我称为 Register.java 的新 Activity

在 Register.java 中,我使用 register_menu.xml 作为布局文件,当用户单击注册按钮时,将运行以下代码(它从 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;

我从其他 StackOverflow 帖子中获取了一些关于如何使用 Java 生成 HTTP 帖子的代码(Java - 通过 POST 方法轻松发送 HTTP 参数)以及从 Android 开发网站关于网络连接最佳实践(http://developer.android )的代码.com/training/basics/network-ops/connecting.html

这应该可以帮助您开始自己的实现。

于 2014-03-31T13:23:15.197 回答