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);
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());
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());
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.