I'm a total newbie to Android development. I'm using Spring Android's Rest Client (http://www.springsource.org/spring-android) to call a remote API to retrieve JSON as a result. This API works perfectly fine with iOS (ie result is never empty). I used Firefox's Poster plugin to do a test, return is also NOT empty. But when I use it in my Android app, the return body is always empty. I really have no idea why. Any help will be greatly appreciated.
Here's my code:
private class MybookingGetTask extends AsyncTask<String, Void, ResponseEntity<BookingsList>> {
@Override
protected void onPreExecute() {
showLoadingProgressDialog();
}
@Override
protected ResponseEntity<BookingsList> doInBackground(String... params) {
try {
// The URL for making the GET request
final String url = ... url to remote API...;
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(new MediaType("application","json"));
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
// Retrieve the parameter passed into the async task
Log.d(TAG, url);
// Perform the HTTP GET request
ResponseEntity<BookingsList> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), BookingsList.class);
Log.i("JACKSON", "Header:" + response);
Log.i("JACKSON", "data" + Arrays.asList(response.getBody())); // THIS IS ALWAYS EMPTY
Log.i("Status", "status:" + response.getStatusCode()); // THIS IS 200
return response;
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
...
}
My header shows:
<200 OK,{Server=[Apache/2.2], Cache-Control=[no-store, no-cache, must-revalidate, post-check=0, pre-check=0],
Content-Type=[text/html; charset=UTF-8],
Date=[Thu, 24 Jan 2013 02:13:23 GMT], Keep-Alive=[timeout=5, max=100], Expires=[Thu, 19 Nov 1981 08:52:00 GMT], Pragma=[no-cache], Connection=[Keep-Alive], Set-Cookie=[X-Mapping-akaopmho=8751D19574A98A3AD06ADB6EAFED9736; path=/, ocio=nu2jnhkh476eosppi9u5ip6nq7; expires=Sat, 23-Feb-2013 02:13:24 GMT; path=/abms/, X-Mapping-akaopmho=8751D19574A98A3AD06ADB6EAFED9736; path=/], Content-Length=[0], X-Android-Sent-Millis=[1358993603914], X-Android-Received-Millis=[1358993605092]}>
The VERY weird thing here is: in my remote server code, the header is already set with Content-Type: application/json (using php header('Content-type: application/json')). But this header printed out in the log is text/html.
I've also tried replacing my api url with twitter's, and the body show twitter's json result. So what am I doing wrong here?
Thanks a lot!!!
Joseph