In Android web service using POST method of REST TEMPLATE, I am trying to send an object to server which contains few parameters and and 2 Objects. Those 2 Objects contains few parameters and 3 Lists of different Objects and each of those 3 Lists of Objects contains few parameters inside them and 1 List of Object that contains a byte array alone. Like in the below pics:
Pic 1[Main Object(The one I am trying to send)]:
Pic 2[Object inside the Main Object comprising 3 Lists of Objects]:
Pic 3[An Object as List comprising 1 List of Another Object inside it, likewise for the other 2(Accommodation & Others)]:
Pic 4[List of Object inside the sub-Object containing a byre array parameter alone]:
And my code for webservice POST method:
public static final String capUrl = "http://192.168.1.7:8084/CAPWS";
public Expenses setExpensesByBatch(Expenses expenses) {
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
list.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(list);
String b = restTemplate.postForObject(capUrl + "/UX/", expenses, String.class);
Log.e("String ===============b", b + " ++++");
} catch (Exception e) {
e.printStackTrace();
Log.e("expObjPost_WsCli_EX", e.toString());
}
return expenses;
}
Whatever I do it returns org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
My LOGCAT
below:
My server side code:
@RequestMapping(value = CapRestURIConstants.UPDATEEXPENSES, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
String updateExpenses(@RequestBody Expenses expenses) {
String response = "true";
System.out.println("incoming--------" + "UPDATEEXPENSES");
try {
if (expenses != null) {
//System.out.println("expenses--------" + expenses);
int catType = expenses.getCategoryType();
String categoryType = String.valueOf(catType);
String categoryId = expenses.getCategoryId();
String batchId = expenses.getBatchId();
System.out.println("categoryType--------" + categoryType);
System.out.println("categoryId--------" + categoryId);
System.out.println("batchId--------" + batchId);
Batch batch = getBatchById(batchId);
if (batch != null) {
expenseDataNew(batch, expenses);
expenseImagesNew(batch, expenses);
}
}
} catch (Exception e) {
e.printStackTrace();
response = "faslse";
}
return response;
}
public void expenseDataNew(Batch batch, Expenses expenses) {
Session session = null;
Transaction transaction = null;
Gson gson = new Gson();
try {
LinkedTreeMap masterMap = (LinkedTreeMap) gson.fromJson(batch.getMasterJson(), Object.class);
LinkedTreeMap expenseObject = (LinkedTreeMap) masterMap.get("2007");
masterMap.replace(expenseObject, expenses);
String masterJson = gson.toJson(masterMap);
batch.setMasterJson(masterJson);
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.saveOrUpdate(batch);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
public void expenseImagesNew(Batch batch, Expenses expenses) {
Session session = null;
Transaction transaction = null;
Gson gson = new Gson();
try {
// DIVERTING TO IMAGE DB
session = ImageHibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
if (expenses != null) {
int catType = expenses.getCategoryType();
String categoryType = String.valueOf(catType);
System.out.println("categoryType--------" + categoryType);
String categoryId = expenses.getCategoryId();
System.out.println("categoryId--------" + categoryId);
String travelExpenseImgId = "f257f225-41da-11e7-be05-001d92ba9634";
String accommodationExpenseImgId = "204b4baf-41db-11e7-be05-001d92ba9634";
String otherExpenseImgId = "45d31872-41db-11e7-be05-001d92ba9634";
AssessorExpense assessorExpense = expenses.getAssessorExpense();
TCExpense tCExpense = expenses.getTcExpense();
Boolean isTravel, isAccomadation, isOthers;
List<Travel> travelList;
List<Accomadation> accommodationList;
List<Others> othersList;
ArrayList proof;
Expense expense;
ExpenseImage expenseImage;
// categoryType: 1 = Assessor
// categoryType: 4 = TC
if (categoryType.equals("1")) {
if (assessorExpense != null) {
isTravel = assessorExpense.isTravel();
if (isTravel) {
travelList = (ArrayList) assessorExpense.getTravel();
System.out.println("travelList.size()==" + travelList.size());
expense = getExpense(travelExpenseImgId);
System.out.println("expense==travel==1==:" + expense);
String expenseStr = String.valueOf(expense);
System.out.println("expenseStr==travel==1==:" + expenseStr);
for (Travel travel : travelList) {
List<ExpenseImageObject> expenseImageObjects = (ArrayList) travel.getExpenseImageObjects();
System.out.println("travel==expenseImageObjects.size()== :" + expenseImageObjects.size());
for (ExpenseImageObject image : expenseImageObjects) {
System.out.println("expense===travel==for==:" + expense);
expenseImage = new ExpenseImage();
expenseImage.setBatchId(batch.getBatchId());
expenseImage.setCategoryId(expenses.getCategoryId());
expenseImage.setExpimgId(expenseStr);
expenseImage.setImage(image.getImage());
expenseImage.setStatus(1); // dummy status 1 - Travel, 2 - Accommodation, 3 - Other
session.saveOrUpdate(expenseImage);
}
}
}
}
}
}
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
I am actually trying to send an object with lists of data as well list of photos in each list to the server.
Browsed through several websites and almost all topics related to the error on Stack Overflow
, Been sitting on this issue for more than a week. Hope I would find some help.
After a hell lot of research and experts suggestions, I finally managed to find the solution to my problem. The problem is not with the code. Its actually perfect. The problem is with the Objects I use to get & set. I am using NetBeans for server side coding. What I did was, I created my necessary Objects in Android studio, copied the parameters, getters & setters and pasted them on NetBeans, Object class. The problem was with the booleans I've been using. The booleans that are created on Android Studio didn't support on NetBeans in some cases, that prevented hitting the server. And after getting expert advice, I did the Object creation on NetBeans copied the parameters, getters & setters and pasted them on Android Studio Object class. And then it worked well. Took me more than 2 weeks to solve this issue. Hope this would be of some help.