我正在開發一個使用谷歌端點和谷歌應用程序引擎的Android應用程序。我的後端似乎並沒有做任何事情。看起來好像沒有任何東西被保存到數據存儲區,因此沒有任何東西可以從中檢索。谷歌應用程序引擎實體沒有被創建
下面是一些API方法我已經寫在端點不能工作:
private static String getUserId(User user) {
String userId = user.getUserId();
if (userId == null) {
AppEngineUser appEngineUser = new AppEngineUser(user);
ofy().save().entity(appEngineUser).now();
// Begin new session for not using session cache.
Objectify objectify = ofy().factory().begin();
AppEngineUser savedUser = objectify.load().key(appEngineUser.getKey()).now();
userId = savedUser.getUser().getUserId();
}
return userId;
}
@ApiMethod(name = "saveProfile", path = "profile", httpMethod = ApiMethod.HttpMethod.POST)
public Profile saveProfile(final User user, final ProfileForm profileForm) throws UnauthorizedException {
if(user == null) {
throw new UnauthorizedException("Authorization required.");
}
String firstName = profileForm.getFirstName();
String surname = profileForm.getLastName();
String userEmail = user.getEmail();
int year = profileForm.getYear();
int month = profileForm.getMonth();
int day = profileForm.getDay();
Profile profile = ofy().load().key(Key.create(Profile.class, getUserId(user))).now();
if (profile == null) {
// the user does not have a profile and is creating one for the first time
profile = new Profile(getUserId(user), firstName, surname, userEmail, year, month, day);
} else {
profile.update(firstName, surname, userEmail, year, month, day);
}
ofy().save().entity(profile).now();
return profile;
}
@ApiMethod(name = "getProfile", path = "profile", httpMethod = ApiMethod.HttpMethod.GET)
public Profile getProfile(User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException("Authentication required.");
}
return ofy().load().key(Key.create(Profile.class, getUserId(user))).now();
}
}
型材類有@Entity
註解,用客觀化靜態塊,像這樣註冊:
static {
factory().register(AppEngineUser.class);
factory().register(Profile.class);
}
用戶標識是由通過GAE
com.google.appengine.api.users.User
0123產生
並且userId屬性是帶有@Index
註釋的字符串。
我也對api explorer感到困惑,它是如何響應這些方法的。每當我調用saveProfile api方法時,一個配置文件對象返回userId
的0
和一封電子郵件"[email protected]"
,但我相信這是在本地主機上運行時的默認電子郵件。
我也通過HTTP運行API瀏覽器,谷歌表示這可能會導致問題。這是沒有任何工作的原因。我不得不加載不安全的腳本,只是爲了使用我的api,但可能它不起作用,因爲它是通過HTTP代替HTTPS託管的。
由於我對GAE的理解存在根本性缺陷,或者是由於我在本地主機上運行,是不是能夠完全測試我的方法的整個問題。如果是後者,也許我應該部署到Appspot,並且事情可能會更流暢。
如果有什麼額外的需要幫助,請提問。
謝謝!