I've been trying to set up a backend for my app using the Google Cloud Endpoints and everything seemed to be going well until I actually tried inserting entities into the datastore.
我一直在尝试使用Google Cloud Endpoints为我的应用设置后端,在我实际尝试将实体插入数据存储区之前,一切似乎都进展顺利。
I'm trying to insert the GenericBike objects to the datastore using the generated endpoints and while the code compiles and seems to runs successfully, nothing is ever actually inserted when I check the project webpage.
我正在尝试使用生成的端点将GenericBike对象插入数据存储区,当代码编译并且似乎成功运行时,在检查项目网页时实际上没有插入任何内容。
Here is my code for actually inserting.
这是我实际插入的代码。
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;
}
}
And here is where I call it
这就是我称之为的地方
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);
}
}
In case it helps, here is my GenericBike entitiy.
如果它有帮助,这是我的GenericBike权利。
@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;
}
EDIT: Here is the generated endpoint code for the insert() method
编辑:这是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();
}
1 个解决方案
#1
1
Looks like you are not launching the request from your client. I believe you need to add .execute() to your client-lib object to actually kick off the request.
您似乎没有从客户端发送请求。我相信您需要将.execute()添加到您的client-lib对象以实际启动请求。
replace
更换
service.insert(bike);
with
同
service.insert(bike).execute();
Also, checking your App Engine logs is a good starting point to confirm that the request actually went through.
此外,检查App Engine日志是确认请求实际通过的良好起点。
#1
1
Looks like you are not launching the request from your client. I believe you need to add .execute() to your client-lib object to actually kick off the request.
您似乎没有从客户端发送请求。我相信您需要将.execute()添加到您的client-lib对象以实际启动请求。
replace
更换
service.insert(bike);
with
同
service.insert(bike).execute();
Also, checking your App Engine logs is a good starting point to confirm that the request actually went through.
此外,检查App Engine日志是确认请求实际通过的良好起点。