how to post multiple edittext data to a webpage with jSessionId

蓝咒 提交于 2019-12-12 05:14:51

问题


I'm new to Android development.

Basically I need to submit data from 3 editTexts of my android app to a webpage and display the response returned. The webpage uses sessions with jSessionId.

I don't have any idea how to post request to the webpage,get the cookie,store it and use it while sending the above said data by attaching the jSessionId to the url. I've been searching about this from past 2 days. but couldn't get anything to work. No idea where to start and how to.

A detailed answer is what I'm looking because I've tried answers from stackoverflow also. Still no luck

I've managed to retrieve the jsessionid so far.

 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;
    }

Now how do I use this sessionid and send the edittext data to retrieve informaion?


回答1:


You have to do in three ways

1) First store your sessionId in database(Backend) on server side from web site (Web client). I don't know what environment your using for web site but You can do it through the Ajax request or other ways if you know.

2) Create the web services which will access the that stored data from database.

3)Finally you will get and post data through android client. Please refer the following code:

  /**
 * 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;

}

This method will return the string response from the server. You can parse the reply and use in your app.




回答2:


Used Cookimanager to set default Cookie for the entire connection. And my problem is solved. Just appended the query parameters using URI.builder()

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


来源:https://stackoverflow.com/questions/34870068/how-to-post-multiple-edittext-data-to-a-webpage-with-jsessionid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!