2013-05-22 38 views
0

我對App Engine和Java Development非常新,嘗試通過Http Post接收InputStream並將其存儲到數據存儲中。將HTTPServletRequest流寫入Google App Engine的數據存儲

要測試這個,我使用另一臺計算機的正常運行時間並在一個流中發送10個正常運行時間值。

每當我測試這個,機器就會變成COUNT值爲10,正常運行時間值的響應。但數據存儲查看器只存儲一個項目。我究竟做錯了什麼?我不認爲這對於使用Memcache的數據有很大的影響,儘管它是更好的方式,我會在稍後再做。

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 
    Entity uptime = new Entity("Uptime"); 

    BufferedReader buff = req.getReader(); 
    String line = buff.readLine(); 

    PrintWriter out = resp.getWriter();  

    int n = 0; 
    Date timestamp = new Date(); 

    while (line != null){ 
     uptime.setProperty("timestamp", timestamp); 
     uptime.setProperty("value", line); 
     datastore.put(uptime); 
     //Ouput for Debug purpose 
     out.println("COUNT: " + n + " LINE: " + line); 
     n++; 

     line = buff.readLine(); 
    } 
} 

回答

2

沒有創建,當你循環,只是改變現有一個一個新的屬性Entity對象。

while (line != null){ 
    Entity uptime = new Entity("Uptime"); // remove the declaration/initialization from before, create a new every loop 
    uptime.setProperty("timestamp", timestamp); 
    uptime.setProperty("value", line); 
    datastore.put(uptime); 
    //Ouput for Debug purpose 
    out.println("COUNT: " + n + " LINE: " + line); 
    n++; 

    line = buff.readLine(); 
} 

我不知道你的數據存儲如何識別對象(是它只是一個地圖,什麼是對象ID,這是什麼它equals()方法嗎?),但它應該區分這種方式。

+0

我的天啊。所以該死的簡單,我一整天都看不到它。非常感謝。我肯定會一次又一次讀取Datastore文檔。 – gizmo

相關問題