Google Cloud Endpoint 插入无法从客户端运行

Google Cloud Endpoint insert not working from client

我一直在尝试使用 Google Cloud Endpoints 为我的应用程序设置后端,在我实际尝试将实体插入数据存储区之前,一切似乎都很顺利。

我正在尝试使用生成的端点将 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 实体。

@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();
}

您似乎没有启动客户端的请求。我相信您需要将 .execute() 添加到您的 client-lib 对象中才能真正启动请求。

替换

service.insert(bike);

service.insert(bike).execute();

此外,检查您的 App Engine 日志是一个很好的起点,可以确认请求确实已通过。