Reputation: 1092
I'm implementing a RESTful service hosted in GAE trying to save Data to the Datastore.
@Path("/db")
public class DBAccess {
private static final Logger log = Logger.getLogger(DBAccess.class.getName());
// Allows to insert contextual objects into the class,
// e.g. ServletContext, Request, Response, UriInfo
@Context
UriInfo uriInfo;
@Context
Request request;
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_JSON)
public void saveDataBase(final List<User> data,
@Context HttpServletResponse servletResponse,
@Context HttpServletRequest servletRequest) throws IOException {
log.info("saveDatabae REST Service has been called.");
Key dbKey = KeyFactory.createKey("DataBase", "default");
Entity user;
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
for (int i = 0; i < data.size(); i++) {
user = new Entity("User", dbKey);
user.setProperty("idUser", data.get(i).getIdUser());
user.setProperty("idGMC", data.get(i).getIdGMC());
datastore.put(user);
}
servletResponse.sendRedirect("../index.jsp");
}
}
On the other hand I have an Android App trying to consume it like
private static void post(String endpoint, Map<String, String> params)
throws IOException, JSONException {
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
Log.v(TAG, "Posting params to " + endpoint);
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(endpoint);
post.setHeader("content-type", "application/json");
// Build JSON
JSONObject dato = new JSONObject();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
dato.put(param.getKey(), param.getValue());
}
StringEntity entity = new StringEntity(dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
Log.v(TAG, "HttpResponse is " + respStr);
}
which is called by
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
post("http://pickapp-server.appspot.com/rest/db", params);
In the GAE admin console I get the following log entry:
The point is, the User Entity is never created in the GAE Datastore. I guess the REST Service is not being called. Can anyone tell me what am I missing?
Upvotes: 0
Views: 500
Reputation: 80330
It seems your REST method expects an array of JSON objects, while Android POST provides one JSON object. Try wrapping it in JSONArray
.
Upvotes: 1