简体   繁体   English

从Android调用Google Cloud端点时注入实体对象

[英]Injecting entity object while calling google cloud endpoint from android

How do I pass the arraylist to google cloud endpoint? 如何将arraylist传递给Google Cloud端点? It doesn't seem to work. 它似乎不起作用。

Edit start ---- Here is the signature of the endpoint with arraylist as input 编辑开始----这是使用arraylist作为输入的端点的签名

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. 因此,即使我传递了10个字符串,端点中也只有一个。

Edit end ---- 编辑结束----

I read somewhere that I should create a wrapper entity for arraylist and then pass it. 我读过某个地方,我应该为arraylist创建包装器实体,然后将其传递。

Edit start ---- So I created the entity containing the arraylist 编辑开始----所以我创建了包含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? 即使不是@Named,也可以从客户端传递DeviceList的对象吗? 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. 我的理解是,由于它是一个实体,因此不能使用@Named命名,因此在调用时我需要注入它。 But google allows only three injected types - 1. com.google.appengine.api.users.User 2. javax.servlet.http.HttpServletRequest 3. javax.servlet.ServletContext 但是Google只允许三种注入的类型-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? 但是我不确定如何从android客户端调用此终结点? How I do pass the entity object using HTTPServletRequest? 如何使用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. 您可以阅读本文以刷新云终结点API和android。

If you are having trouble passing an arraylist of objects from your client to the API, I would suggest checking some things: 如果您在将对象的数组列表从客户端传递到API时遇到问题,建议您检查一下以下内容:

  • does the argument type in the API match what is being sent from the client? API中的参数类型是否与客户端发送的内容匹配? 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? 如果arraylist内的数据类型是原始数据,并且仍然失败,则可能是您在“某处”读到的建议是指需要使用仅包含一个包装对象的包装对象(尽管我认为不是这种情况)字段-arraylist,并沿线传递?

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. 如果提醒您检查日志/错误消息或此答案中的任何其他信息帮助您解决了问题,请记住接受它,但是请务必先编辑您的帖子以说明如何解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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