【问题标题】:Error fetching entity by Entity Id from Google App Engine backend for Android app从适用于 Android 应用的 Google App Engine 后端按实体 ID 获取实体时出错
【发布时间】:2014-02-18 05:45:20
【问题描述】:

我使用 Google App Engine 作为我的 Android 应用程序的后端。我可以使用该方法将实体插入数据存储区(在我为 Note 类创建端点时自动生成)

/**
 * 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 note the entity to be inserted.
 * @return The inserted entity.
 */
@ApiMethod(name = "insertNote")
public Note insertNote(Note note) {
    EntityManager mgr = getEntityManager();
    try {
        if (containsNote(note)) {
            throw new EntityExistsException("Object already exists");
        }
        mgr.persist(note);
    } finally {
        mgr.close();
    }
    return note;
}

因此,要从我的 Android 应用程序中使用此方法并插入一个实体,我将使用我的 AsyncTask class 中的此方法

try {
      Note note = new Note();

      String descrptn = descriptionTF.getText().toString();
      String email = emailTF.getText().toString();
      String noteID = new Date().toString();

      note.setDescription(descrptn);
      note.setId(noteID);
      note.setEmailAddress(email);      

      Note result = endpoint.insertNote(note).execute();

  } catch (IOException e) {
    e.printStackTrace();
  }

在我尝试从数据存储中检索实体之前,一切都是阳光和彩虹。我得到FATAL Exception:...Caused by Java.lang.NullPointerException: required parameter id must be specified

这是尝试检索实体时调用的方法(也是自动生成的)

/**
 * This method gets the entity having primary key id. It uses HTTP GET method.
 *
 * @param id the primary key of the java bean.
 * @return The entity with primary key id.
 */
@ApiMethod(name = "getNote")
public Note getNote(@Named("id") String id) {
    EntityManager mgr = getEntityManager();
    Note note = null;
    try {
        note = mgr.find(Note.class, id);
    } finally {
        mgr.close();
    }
    return note;
}

这就是我从 Android 活动中尝试的内容

 try {
      Note newNote = new Note();
      String noteID = newNote.getId();
      Note newResult = endpoint.getNote(noteID).execute();
      String descrptn = newResult.getDescription();
      String email = newResult.getEmailAddress(); 


      descriptionTV.setText(descrptn); 
      emailTV.setText(email);


  } catch (IOException e) {
    e.printStackTrace();
  }
      return (long) 0;
    }

谁能帮我解决这个问题?

【问题讨论】:

    标签: java android google-app-engine jpa


    【解决方案1】:

    在您的 Android 活动中,我看到以下代码:

      Note newNote = new Note();
      String noteID = newNote.getId();
      Note newResult = endpoint.getNote(noteID).execute();
    

    如果您看到,您正在第一行创建 Note() 类的实例。这仍然意味着 Note 类的其他属性(包括Id)为空。因此,实际上在您的第二行中,您将 null 分配给 noteID 并将其传递给端点 getNote 方法。因此,服务器端的 Id 为 null,从而引发异常。

    在您的 Android 代码中,您可能会在某些 ListActivity 中显示注释列表,然后当您选择其中一个注释时,您将能够从 selectedItem 中获取 ID。因此,您应该传递此值,以从服务器端点实现中检索详细信息。

    希望这能让事情变得清晰。

    【讨论】:

    • 嗨。但是 getId() 方法应该返回数据存储中已经存在的 id。
    • 请再看一遍代码。您正在那里实例化一个类,但尚未初始化任何内容。
    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多