简体   繁体   中英

Injecting entity object while calling google cloud endpoint from android

How do I pass the arraylist to google cloud endpoint? It doesn't seem to work.

Edit start ---- Here is the signature of the endpoint with arraylist as input

public CollectionResponse<String> listDevices(@Named("devices") ArrayList<String> devices) 

However when I iterate over this arraylist, I get all the records condensed into one. So even though I pass 10 strings, I get only one in my endpoint.

Edit end ----

I read somewhere that I should create a wrapper entity for arraylist and then pass it.

Edit start ---- So I created the entity containing the arraylist

@Entity
public class DEviceList {
    ArrayList<String> devices;
}

and modified the signature as -

public CollectionResponse<String> listDevices(DeviceList devices) 

Is it possible to pass object of DeviceList from client even though it not @Named? Can you provide the syntax? My understanding is that since it's an entity it cannot be @Named, so while calling I need to inject it. But google allows only three injected types - 1. com.google.appengine.api.users.User 2. javax.servlet.http.HttpServletRequest 3. javax.servlet.ServletContext

So above signature would not work.

So I changed the signature to -

public CollectionResponse<String> listDevices(HttpServletRequest request)

and inside I could get the entity as

DeviceList deviceList = (DeviceList)request.getAttribute("deviceList");

However I am not sure how to call this endpoint from the android client? How I do pass the entity object using HTTPServletRequest?

Edit end ----

How do I do that? Can anyone cite an example?

The way you word your question, it seems the call is silently failing. That can't be. You must be receiving some kind of exception or log somewhere which could help you identify your issue better. You could read this article to refresh on cloud endpoint APIs and android.

If you are having trouble passing an arraylist of objects from your client to the API, I would suggest checking some things:

  • does the argument type in the API match what is being sent from the client? Do they both have access to the class definition?
  • if the data type inside the arraylist is a primitive, and it still fails, perhaps the advice you read "somewhere" was referring to the need (although I don't think this is the case) to use a wrapper object which simply contains one field - the arraylist, and pass that along the line?

If either the reminder to check your logs/error messages or any other info in this answer helped you resolve the issue, please remember to accept it, but not without first editing your post to explain how you resolved your issue.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM