java - 从 Android 应用程序的 Google App Engine 后端按实体 ID 获取实体时出错

标签 java android google-app-engine jpa

我使用 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 类

中使用此方法
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();
  }

一切都是阳光和彩虹,直到我尝试从数据存储中检索实体。我得到致命异常:...由Java.lang.NullPointerException引起:必须指定必需的参数id

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

/**
 * 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 Activity 中尝试的内容

 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;
    }

谁能帮我解决这个问题吗?

最佳答案

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

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

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

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

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

关于java - 从 Android 应用程序的 Google App Engine 后端按实体 ID 获取实体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21845301/

相关文章:

java - Android JSON转换问题

java - 两个包装器对象如何相等,但同时不相等

google-app-engine - Google App Engine 中的 Memcache 1 MB 限制

javascript - 如何设置 GAE 数据存储的 key ?

java - 如何在 3 个示例中打印 jasper 报告且几乎没有任何变化?

java - 在一行中创建项目列表,然后查询列表以查看项目是否存在,而不会在 Java 中收到未经检查的转换警告

php - 上传图片到服务器php-my-sql(android)

java - 为什么我无法在 Android 中创建 *.txt 文件?

android - 如何在Android上使用FaceDetector.Face进行人脸识别

google-app-engine - 为什么 5 年后我的 Java appengine 应用程序上有 "500 Server Error"而没有任何问题?