Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将自动生成的实体密钥重新保存到google app engine端点中?_Android_Google App Engine_Key_Entity_Google Cloud Endpoints - Fatal编程技术网

Android 如何将自动生成的实体密钥重新保存到google app engine端点中?

Android 如何将自动生成的实体密钥重新保存到google app engine端点中?,android,google-app-engine,key,entity,google-cloud-endpoints,Android,Google App Engine,Key,Entity,Google Cloud Endpoints,我们正在用GAE实现一个android项目,我们在AppEngine项目中创建了一个实体,我们希望在客户端使用它。我们需要检索刚刚创建的实体的密钥 实体代码: @Entity public class Poll { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Key keyPoll; private String title; private String creator; private Date creat

我们正在用GAE实现一个android项目,我们在AppEngine项目中创建了一个实体,我们希望在客户端使用它。我们需要检索刚刚创建的实体的密钥

实体代码:

@Entity
public class Poll {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key keyPoll;
private String title;
private String creator;
private Date creationDate;
private String close;

public Key getKeyPoll() {
    return keyPoll;
}

public String getTitle() {
    return title;
}

public String getCreator() {
    return creator;
}

public Date getCreationDate() {
    return creationDate;
}

public String getClose() {
    return close;
}

public void setTitle(String title) {
    this.title = title;
}

public void setCreator(String creator) {
    this.creator = creator;
}

public void setCreationDate(Date date) {
    creationDate = date;
}
public void setClose(String state) {
    close = state;
}
}
客户端代码:

  private class PollTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

                  //Entity creation
          Poll poll = new Poll();


                  poll.setCreator("Bill");
          poll.setCreationDate(new DateTime(System.currentTimeMillis()));
          poll.setTitle(title);
          poll.setClose("n");

          Pollendpoint.Builder builder = new Pollendpoint.Builder(
              AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
              null);

          builder = CloudEndpointUtils.updateBuilder(builder);

          Pollendpoint endpoint = builder.build();


          try {
            endpoint.insertPoll(poll).execute();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }

                  //this is another entity that store in its field "pollK" the key of
                  // the previous entity "poll"
          PollImg pollImg = new PollImg();


          pollImg.setPollK(poll.getKeyPoll());
          pollImg.setImageK(imageKey);

          Pollimgendpoint.Builder imgBuilder = new Pollimgendpoint.Builder(
              AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
              null);

          imgBuilder = CloudEndpointUtils.updateBuilder(imgBuilder);

          Pollimgendpoint imgEndpoint = imgBuilder.build();


          try {
            imgEndpoint.insertPollImg(pollImg).execute();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }


          return null;
        }
      }

更正答案:根据您显示的代码,对象
poll
是您首先创建的本地对象,然后通过网络将其发送到App Engine后端。您的
insertPoll
方法看起来不错


但是,您不能期望代码自动填充
poll
对象的键字段,因为该对象仍然是本地的,仅此而已。要检索实际值,您需要查看
endpoint.insertPoll(poll.execute())的响应值在您的代码中。这将返回Poll对象,然后可以查询该对象以获取键值

谢谢你的回答,我发布了insertPoll方法的代码。
/**
 * This inserts a new entity into App Engine datastore. If the entity already
 * exists in the datastore, an exception is thrown.
 * It uses HTTP POST method.
 *
 * @param poll the entity to be inserted.
 * @return The inserted entity.
 */
@ApiMethod(name = "insertPoll")
public Poll insertPoll(Poll poll) {
    EntityManager mgr = getEntityManager();
    try {

        mgr.persist(poll);
    } finally {
        mgr.close();
    }
    return poll;
}