use jackson library to create json string from array list of class object and after that send it. I have done successfully with this.
public String sendArrayListOfuserclass() {
String request_string = null;
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
HttpResponse response;
try {
HttpPost post = new HttpPost(
ServiceConstant.ALL_PERSON_UPDATE_DETAIL_URL);
post.setHeader("Content-type", "application/x-www-form-urlencoded");
DatabaseHelper mDatabaseHelper = DatabaseHelper
.getInstance(sContext);
List<PersonInformationBean> allPersonDetailList = mDatabaseHelper
.getAllPersonDetail();
List<ActivationBean2> PersonRequest = new ArrayList<ActivationBean2>();
int size = allPersonDetailList.size();
if (size > 0) {
for (PersonInformationBean personInformationBean : allPersonDetailList) {
ActivationBean2 personactivationdata = new ActivationBean2();
try {
personactivationdata.setPersonID(personInformationBean
.getPersonId());
personactivationdata
.setActivationKey(personInformationBean
.getActivationKey());
} catch (Exception e) {
}
PersonRequest.add(personactivationdata);
}
}
RefreshPersonBean personwithdeviceid = new RefreshPersonBean();
personwithdeviceid.setPersonRequest(PersonRequest);
personwithdeviceid.setDeviceID(MyPreferences.getInstance(sContext)
.getDeviceId());
request_string = convertBeanToJson(personwithdeviceid);
request_string = request_string.replace("\\", "")
.replace("\"[", "[").replace("]\"", "]");
request_string = request_string.replace("\n", "").replace("\r", "");
Log.e("referesh person request", request_string);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair(
ServiceConstant.RequestData, request_string));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
// post.setEntity(new ByteArrayEntity(json.toString().getBytes()));
response = client.execute(post);
/* Checking response */
if (response != null) {
InputStream inputStream = response.getEntity().getContent(); // Get
request_string = convertStreamToString(inputStream);
Log.e("response",
"response of post = " + request_string.toString());
return request_string;
}
} catch (IOException e) {
request_string = "timeout";
e.printStackTrace();
} catch (Exception e) {
// Toast.makeText(sContext, "Server Error",
// Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return request_string;
}
this is jackson library function:
private String convertBeanToJson(RefreshPersonBean bean) {
String reqQuery = null;
try {
reqQuery = mapperObj.writeValueAsString(bean);
Log.d("convertBeanToJson(): request query is:", reqQuery);
} catch (JsonGenerationException e) {
Log.e("convertBeanToJson():", " caught JsonGenerationException");
} catch (JsonMappingException e) {
// mELogger.error("convertBeanToJson(): caught JsonMappingException");
} catch (IOException e) {
Log.e("convertBeanToJson():", " caught IOException");
}
return reqQuery;
}//
library name jackson-all-1.6.2