簡體   English   中英

無法從客戶端使用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();
}

看起來您沒有從客戶端啟動請求。 我相信您需要向客戶端lib對象添加.execute()才能真正啟動請求。

更換

service.insert(bike);

service.insert(bike).execute();

另外,檢查您的App Engine日志也是確認請求確實通過的一個很好的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM