我目前正在研究一個項目,要求我們存儲和接收來自服務器的食譜。食譜以json格式存儲在服務器ip + an id中。我一直在考慮創建一個可以保證在我們提交的兩個食譜之間不會重疊的ID的好方法。是否有一種標準的例外方式來創建這些ID,還是僅存儲一個最大ID的int保持軌跡並拉動每當有人想要存儲配方服務器端時更好?爲服務器端存儲創建ID的好方法?
由於提前,
優雅
public void addRecipe(Recipe recipe, String URL) throws IllegalStateException, IOException{
// TODO Implement the server-side ID tracking
//int id = getID();
//recipe.setId(id);
HttpPost httpPost = new HttpPost(URL+recipe.getId());
StringEntity stringEntity = null;
try {
stringEntity = new StringEntity(gson.toJson(recipe));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpPost.setHeader("Accept","application/json");
httpPost.setEntity(stringEntity);
HttpResponse response = null;
response = httpclient.execute(httpPost);
// String status = response.getStatusLine().toString();
// System.out.println(status);
HttpEntity entity = response.getEntity();
try {
// May need if statement, check isStreaming();
entity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//May possibly need to deallocate more resources here. No 4.0 implementation of releaseconnection();
}
也許這個代碼將有助於?這是我將食譜存儲在服務器上的方式。
您的數據如何存儲? SQL db中的'autoincrement'列可以解決這個問題,例如 – 2013-03-16 04:41:49
我不完全確定服務器端是爲我們設置的,我們只需要從中拉出並搜索,但我相信它是elasticsearch?如果這是有道理的? – Classtronaut 2013-03-16 04:48:09
我想我們需要看到一些代碼和更多的上下文來幫助你。 – 2013-03-16 04:49:44