0

i have to send This JsonArray With HTTP Request from client to sever and have to fetch it on to servlet page..without NameValuePair Class because my requirement is diffrent.

any help would be appreciated.

hear is some code i was using to send parameters but this time its jsonArray so i cant use it

   Map<String, String> params = new HashMap<String, String>();
   params.put(Constants.NAME, name);

and then building the body.

 StringBuilder bodyBuilder = new StringBuilder();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
// constructs the POST body using the parameters
while (iterator.hasNext()) {
    Entry<String, String> param = iterator.next();
    bodyBuilder.append(param.getKey()).append('=')
            .append(param.getValue());
    if (iterator.hasNext()) {
        bodyBuilder.append('&');
    }
}
String body = bodyBuilder.toString();

and then HTTP Request.

 conn = (HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-forurlencoded;charset=UTF-8");
        // post the request
        OutputStream out = conn.getOutputStream();

        out.write(bytes);
3
  • You're not going to get a answer to this question without a better description of the question and some kind of code example. Please read this stackoverflow.com/faq and reformat your question Commented Mar 11, 2013 at 8:00
  • i knew that is going to come..specially for you sir see the code..plzz.:-) Commented Mar 11, 2013 at 8:03
  • 1
    Why don't you use Google's gson library to parse your json. Send it as a string from the client side via the HTTP Request and read your json string on the server. This is what I usually use and its the fastest and easiest. You can also encrypt the string easily if that is needed. Commented Mar 11, 2013 at 8:20

2 Answers 2

3

This way you can send JSON array to Server

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

StringEntity se = new StringEntity(jsonArray.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);

HttpResponse response = httpclient.execute(httppost);

Servlet you can read json array like this (Use this code inside doPost method in Servlet):

StringBuilder sb = new StringBuilder();
BufferedReader br = request.getReader();
String str;
while( (str = br.readLine()) != null ){
    sb.append(str);
}    
JSONArray jArr = new JSONArray(sb.toString());
Sign up to request clarification or add additional context in comments.

5 Comments

How Can I receive it on servlet?
Use the second code snippet inside doPost method in the servlet
I have send JsonArray in The Form Of String Using...params.put("json", jsonArray.toString()); and how can again convert it to JsonArray??
Ok then you can retrieve json string as a request parameter, then create a json array object with json string JSONArray arr = new JSONArray(jsonstr); you may need to add this jar to your web-inf lib folder: software.dzhuvinov.com/json-rpc-2.0-client.html
ok...Got It??Actually i did'nt wanted to do bit of extra work thats why i was asking for a solution...thanks you helped me great..i got it working..
1

Ahhhhhh...Skip bit of extra work..for those who understand my question i am posting answer ...using that method what i have mentioned in the question you can simply receive JsonArray to Servlet..

put this into params as i mentioned

params.put("json", jsonArray.toString());

and then for receivng in servlet..

    String jsonArray=request.getParameter("json");
    JSONArray jArr = new JSONArray(j.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.