我一直在嘗試使用Google雲端點爲我的應用設置後端,並且在我實際嘗試將實體插入數據存儲區之前,一切似乎都進展順利。Google雲端點插入無法從客戶端運行
我想使用生成的端點將GenericBike對象插入到數據存儲中,並且代碼編譯並似乎成功運行時,在檢查項目網頁時沒有實際插入任何內容。
這是我實際插入的代碼。
private class AddGenericBikeAsyncTask extends AsyncTask<GenericBike, Void, Void> {
@Override
protected Void doInBackground(GenericBike... params) {
GenericBikeApi.Builder builder = new GenericBikeApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
.setRootUrl("https://banded-coder-125919.appspot.com/_ah/api/");
GenericBikeApi service = builder.build();
try {
GenericBike bike = params[0];
Log.e("Debug", "Inserting bike...");
service.insert(bike);
Log.e("Debug", "Done inserting bike.");
} catch (IOException e) {e.printStackTrace(); }
return null;
}
}
而這裏就是我稱之爲
if (mGenericBikes == null) { // we need to preload the database still
for (int i=0; i<10; i++) {
GenericBike bike = new GenericBike();
bike.setId(new Long(i+1));
new AddGenericBikeAsyncTask().execute(bike);
}
}
萬一有幫助,這是我的GenericBike entitiy。
@Entity
public class GenericBike {
@Id Long mId;
boolean mAtStation;
public GenericBike() {
mAtStation = true;
}
public Long getId() {
return mId;
}
public void setId(Long id) {
mId = id;
}
public boolean isAtStation() {
return mAtStation;
}
public void setAtStation(boolean atStation) {
mAtStation = atStation;
}
編輯:下面是insert()方法生成的端點代碼
/**
* Inserts a new {@code GenericBike}.
*/
@ApiMethod(
name = "insert",
path = "genericBike",
httpMethod = ApiMethod.HttpMethod.POST)
public GenericBike insert(GenericBike genericBike) {
// Typically in a RESTful API a POST does not have a known ID (assuming the ID is used in the resource path).
// You should validate that genericBike.mId has not been set. If the ID type is not supported by the
// Objectify ID generator, e.g. long or String, then you should generate the unique ID yourself prior to saving.
//
// If your client provides the ID then you should probably use PUT instead.
ofy().save().entity(genericBike).now();
logger.info("Created GenericBike.");
return ofy().load().entity(genericBike).now();
}
小心描述問題? – Gendarme
@Gendarme,對不起,我的大腦現在有點油炸了。我試圖使用端點將GenericBike對象插入到數據存儲中,並且在代碼編譯並似乎成功運行時,在檢查項目網頁時沒有實際插入任何內容。 – noblera
只有當您發佈「GenericBikeApi.Insert」方法代碼時,我們才能弄明白。 –