0

I am trying to send array of boolean values to servlet. This is what I have done so far, and I am so stuck:

HttpClient client = new DefaultHttpClient();  
    String postURL = "http://APP_NAME.appspot.com/listenforrestclient";
    HttpPost post = new HttpPost(postURL);
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        for (int i=0; i<arrBool.length; i++) {
        arrBool[i] = r.nextBoolean();
        String[] user = {"","","","","",""};
        if (arrBool[i] == true) {
            params.add(new BasicNameValuePair("user[i]", arrBool.toString()));
        }
        else if (arrBool[i] == false) {
            params.add(new BasicNameValuePair("user[i]", arrBool.toString()));
    }
        }
        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
        post.setEntity(ent);
        HttpResponse responsePOST = client.execute(post);  
        HttpEntity resEntity = responsePOST.getEntity();  
        if (resEntity != null) {    
            System.out.printf("RESPONSE: ", EntityUtils.toString(resEntity));
        }
} catch (Exception e) {
    e.printStackTrace();
}

I tried to do just user[i], "user[i]", user. Still couldn't find it.

and on servlet I have:

 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException 
{
    resp.setContentType("text/plain");

    for (int i=0; i<mmaa.length; i++) {
        mmaa = req.getParameterValues("user");
        resp.getWriter().println(mmaa[i]);
    }

}

I searched online a lot, couldn't find anything appropriate. I would really appreciate if someone would help me with this.

Thanks

Mac
  • 27
  • 2
  • 9
  • Can you post the BasicNameValuePair() class or the constructor? You have declared `List params = new ArrayList();` but your adding objects of BasicNameValuePair in params is BasicNameValuePair super class of NameValuePair or something? – Lakshmi Feb 28 '13 at 06:25
  • I am able to post BasicNameValuePair("user","Available"), but that is only one value "Available" to String "user". I want to send like an array of users. BasicNameValuePair is a class which is basic implementation of NameValuePair as per the Apache HTTP Client – Mac Feb 28 '13 at 06:29
  • what does the constructor of BasicNameValuePair looks like ???? – Lakshmi Feb 28 '13 at 06:31
  • Constructor would be BasicNameValuePair(String name, String value) – Mac Feb 28 '13 at 06:33
  • I think what you want is a list of NameValuePair and each pair will contain a username as key and the boolean data as value rite ??? check my answer. – Lakshmi Feb 28 '13 at 06:41
  • Yes that's what I want, just the boolean data will be selected randomly. I am trying your answer, I will let you know in a minute – Mac Feb 28 '13 at 06:48

2 Answers2

2

You have params.add(new BasicNameValuePair("user[i]", arrBool.toString()));

But your key is the string "user[i]". Use String.format("user[%d]", i) instead. So, change

params.add(new BasicNameValuePair("user[i]", arrBool.toString()));

to

params.add(new BasicNameValuePair(String.format("user[%d]", i), arrBool.toString()));

In the servlet, you get the parameter values by doing:

for(int i = 0; i < ...){
    String value = request.getParameter(String.format("user[%d]", i);
    //process value  
}
zz3599
  • 647
  • 5
  • 20
  • Thank you for the response. But where should I use String.format("user[%d]",i)? Also I did not understand how to Post those values to servlet so I did that loop. What loop should it be instead. Again I really appreciate your reply. – Mac Feb 28 '13 at 06:16
  • And mmaa is empty String array, I used as String[] mmaa = {"","","","","",""}; – Mac Feb 28 '13 at 06:19
  • The loop is okay. You just need to be consistent with the name you use for the `BasicNameValuePair` and the name you use to retrieve the parameter in the request. See edits – zz3599 Feb 28 '13 at 06:36
  • Thanks. I tried according to you. Previously it was just showing null value or sometimes some garbage value like java.lang.@5444a4a. After edit, it runs well, but doesn't show any output on the servlet – Mac Feb 28 '13 at 06:44
1

You are adding new objects of params.add(new BasicNameValuePair("user[i]", arrBool.toString()));instead of this try doing

params.add(new BasicNameValuePair(user[i].toString(), arrBool.toString()));

In servlet first you need to fetch the array list from the request parameter and for each value in the array list make an object of the same BeanNameValuePair and retrive values from it by using getValue() check Whether you are getting the values properly in the servlet Check first if your getting the ArrayList properly by debugging.

Lakshmi
  • 2,204
  • 3
  • 29
  • 49