简体   繁体   中英

Google Cloud Endpoint insert not working from client

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.

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.

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.

@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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM