【发布时间】: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