2016-03-27 104 views
0

我一直在嘗試使用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(); 
} 
+0

小心描述問題? – Gendarme

+0

@Gendarme,對不起,我的大腦現在有點油炸了。我試圖使用端點將GenericBike對象插入到數據存儲中,並且在代碼編譯並似乎成功運行時,在檢查項目網頁時沒有實際插入任何內容。 – noblera

+0

只有當您發佈「GenericBikeApi.Insert」方法代碼時,我們才能弄明白。 –

回答

1

看起來你是不是從啓動您的客戶端請求。我相信您需要將.execute()添加到您的客戶端lib對象以實際啓動請求。

service.insert(bike).execute(); 

而且取代

service.insert(bike); 

,檢查你的App Engine紀錄是一個很好的起點,以確認該請求實際上經歷。

+0

這就是問題所在。我不確定我是如何錯過它的,因爲我使用list()方法正確地做了它......噢,非常感謝你。你爲我節省了很多頭痛。 – noblera