简体   繁体   中英

pass parameters via http post method

I have two text boxes, 1 for username and the other for password. I wanted to pass what the user enters into the edit texts with the post method

String request = "https://beta135.hamarisuraksha.com/web/webservice/HamariSurakshaMobile.asmx/getIMSafeAccountInfoOnLogon";
                URL url;

                try {
                    url = new URL(request);
                    HttpURLConnection connection = (HttpURLConnection) url
                            .openConnection();
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setInstanceFollowRedirects(false);
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("Connection", "Keep-Alive");
                    connection.setRequestProperty("Content-Type",
                            "application/x-www-form-urlencoded;");// boundary="+CommonFunctions.boundary
                    connection.setUseCaches(false);

                    DataOutputStream wr = new DataOutputStream(
                            connection.getOutputStream());
                    wr.writeBytes(urlParameters);
                    wr.flush();
                    wr.close();


                    int responseCode = connection.getResponseCode();
                    /*
                     * System.out.println("\nSending 'POST' request to URL : " +
                     * url); System.out.println("Post parameters : " +
                     * urlParameters);
                     */
                    System.out.println("Response Code : " + responseCode);

                    InputStream errorstream = connection.getErrorStream();

                    BufferedReader br = null;
                    if (errorstream == null) {
                        InputStream inputstream = connection.getInputStream();
                        br = new BufferedReader(new InputStreamReader(inputstream));
                    } else {
                        br = new BufferedReader(new InputStreamReader(errorstream));
                    }
                    String response = "";
                    String nachricht;
                    while ((nachricht = br.readLine()) != null) {
                        response += nachricht;
                    }

                    // print result
                    // System.out.println(response.toString());
                    return response.toString();

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                } catch (ProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }
            }

if i am getting your question correctly , you need to pass your parameters to a web service. in my case i have implemented a method to get the web service response by giving the url and the values as parameters. i think this will help you.

public JSONObject getJSONFromUrl(JSONObject parm,String url) throws JSONException {


         InputStream is = null;
         JSONObject jObj = null;
         String json = "";
        // Making HTTP request
        try {
            // defaultHttpClient
            /*JSONObject parm = new JSONObject();
            parm.put("agencyId", 27);
            parm.put("caregiverPersonId", 47);*/

        /*  if(!(jObj.isNull("d"))){
                jObj=null;
            }
            */


            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
            HttpEntity body = new StringEntity(parm.toString(), "utf8");
            httpPost.setEntity(body);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            is = httpEntity.getContent();          

               /* String response = EntityUtils.toString(httpEntity);
                Log.w("myApp", response);*/

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

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

     //   JSONObject jObj2 = new JSONObject(json);
        // try parse the string to a JSON object
        try {
             jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

this method take two parameters. one is the url, other one is the values that we should send to the web service. and simply returns the json object. hope this will help you

EDIT

to pass your username and password just use below code

    JsonParser jp = new JsonParser();  // create instance for the jsonparse class

    String caregiverID = MainActivity.confirm.toString();

    JSONObject param = new JSONObject();
    JSONObject job =  new JSONObject();
    try {
        param.put("username", yourUserNAme);
        job = jp.getJSONFromUrl(param, yourURL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM