【问题标题】:how to post multiple edittext data to a webpage with jSessionId如何使用 jSessionId 将多个edittext数据发布到网页
【发布时间】:2016-01-31 00:31:11
【问题描述】:

我是 Android 开发新手。

基本上我需要将我的android应用程序的3个editTexts中的数据提交到网页并显示返回的响应。 该网页使用带有 jSessionId 的会话。

我不知道如何将请求发布到网页、获取 cookie、存储并使用它,同时通过将 jSessionId 附加到 url 来发送上述数据。 从过去的2天开始,我一直在寻找这个。但无法得到任何工作。不知道从哪里开始以及如何开始。

我正在寻找详细的答案,因为我也尝试过来自 stackoverflow 的答案。还是没有运气

到目前为止,我已经设法检索到 jsessionid。

 public boolean send() {
        String givenDob = dobET.getText().toString();
        String givensurname = surnameEt.getText().toString();
        String givenCaptcha = captchaET.getText().toString();
        String url = "https://mysite/getinfo.html";
        try {
            URLConnection connection = new URL(url).openConnection();
            InputStream response = connection.getInputStream();
            List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
            Log.d("Link Response", String.valueOf(response));
            Log.d("Cookie", String.valueOf(cookies));
            String value = cookies.get(0);
            if (value.contains("JSESSIONID")) {
                int index = value.indexOf("JSESSIONID=");

                int endIndex = value.indexOf(";", index);
                String sessionID = value.substring(
                        index + "JSESSIONID=".length(), endIndex);
                Log.d("id " ,sessionID);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

现在我如何使用这个 sessionid 并发送 edittext 数据来检索信息?

【问题讨论】:

    标签: android httpurlconnection jsessionid


    【解决方案1】:

    你必须通过三种方式来做

    1) 首先将您的 sessionId 从网站(Web 客户端)存储在服务器端的数据库(后端)中。 我不知道您的网站使用什么环境,但如果您知道,您可以通过 Ajax 请求或其他方式来完成。

    2) 创建将从数据库中访问存储数据的 Web 服务。

    3) 最后,您将通过 android 客户端获取和发布数据。 请参考以下代码:

      /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
            List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
    
            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }
    
                httpResponse = httpClient.execute(httpPost);
    
            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);
    
                httpResponse = httpClient.execute(httpGet);
    
            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return response;
    
    }
    

    此方法将从服务器返回字符串响应。 您可以解析回复并在您的应用中使用。

    【讨论】:

    【解决方案2】:

    使用Cookimanager 为整个连接设置默认Cookie。我的问题解决了。只需使用URI.builder() 附加查询参数

    URL strUrl = new URL("https://myurl.com/services/getinfo.html?dateOfBirth="
                        + params[0] +
                        "&surName=" + params[1] +
                        "&firstName&middleName" +
                        "&captchaCode=" + params[2]);
    

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多