0

我剛開始學習GAE,現在我正在開發一個使用GAE和eclipse的簡單服務。我使用Objectify創建了3個實體,但是當我訪問http://localhost:8080/_ah/admin/時,Entity Kind組合框是空的。無法在Eclipse中創建GAE實體

這裏是我的物化代碼:

package com.google.training.helloworld; 

//import all entity 
import com.google.training.helloworld.Clients; 
import com.google.training.helloworld.Posts; 
import com.google.training.helloworld.Users; 

//import objectify 
import com.googlecode.objectify.Objectify; 
import com.googlecode.objectify.ObjectifyFactory; 
import com.googlecode.objectify.ObjectifyService; 

/** 
* Custom Objectify Service that this application should use. 
*/ 
public class OfyService { 
    /** 
    * This static block ensure the entity registration. 
    */ 
    static { 
     // register all entity 
     factory().register(Clients.class); 
     factory().register(Posts.class); 
     factory().register(Users.class); 
    } 

    /** 
    * Use this static method for getting the Objectify service object in order to make sure the 
    * above static block is executed before using Objectify. 
    * @return Objectify service object. 
    */ 
    public static Objectify ofy() { 
     return ObjectifyService.ofy(); 
    } 

    /** 
    * Use this static method for getting the Objectify service factory. 
    * @return ObjectifyFactory. 
    */ 
    public static ObjectifyFactory factory() { 
     return ObjectifyService.factory(); 
    } 

} 

這裏的客戶端類代碼:

package com.google.training.helloworld; 

import com.googlecode.objectify.annotation.Entity; 
import com.googlecode.objectify.annotation.Id; 

@Entity 
public class Clients { 

    @Id private String clientApiKey; 
    private String clientName, clientEmail, clientPassword; 

    public Clients(String clientApiKey, String clientName, String clientEmail, 
      String clientPassword) { 
     super(); 
     this.clientApiKey = clientApiKey; 
     this.clientName = clientName; 
     this.clientEmail = clientEmail; 
     this.clientPassword = clientPassword; 
    } 

    public String getClientName() { 
     return clientName; 
    } 

    public void setClientName(String clientName) { 
     this.clientName = clientName; 
    } 

    public String getClientEmail() { 
     return clientEmail; 
    } 

    public void setClientEmail(String clientEmail) { 
     this.clientEmail = clientEmail; 
    } 

    public String getClientPassword() { 
     return clientPassword; 
    } 

    public void setClientPassword(String clientPassword) { 
     this.clientPassword = clientPassword; 
    } 

    public String getClientApiKey() { 
     return clientApiKey; 
    } 

    @Override 
    public String toString() { 
     return "Clients [clientApiKey=" + clientApiKey + "]"; 
    } 



} 

這裏是我的端點代碼:

package com.google.training.helloworld; 

import java.lang.String; 
import java.util.Random; 

import com.google.api.server.spi.config.Api; 
import com.google.api.server.spi.config.ApiMethod; 
import com.google.api.server.spi.config.ApiMethod.HttpMethod; 
import com.google.api.server.spi.config.Named; 
import com.google.training.helloworld.OfyService; 
import com.googlecode.objectify.cmd.Query; 

/** 
* Defines endpoint functions APIs. 
*/ 
@Api(name = "helloworldendpoints", version = "v1", 
scopes = {Constants.EMAIL_SCOPE }, 
     clientIds = {Constants.WEB_CLIENT_ID, Constants.API_EXPLORER_CLIENT_ID }, 
     description = "API for hello world endpoints.") 

public class HelloWorldEndpoints { 

    Random rand = new Random(); 
    OfyService o = new OfyService(); 

    // Declare this method as a method available externally through Endpoints 
    //@ApiMethod(name = "clientLogin", path = "clientLogin", 
    //  httpMethod = HttpMethod.POST) 

    //public String clientLogin(@Named("apikey") String apikey, @Named("password") String pass) { 
    // return "a"; 
    //} 

    // Declare this method as a method available externally through Endpoints 
    @ApiMethod(name = "clientRegister", path = "clientRegister", 
      httpMethod = HttpMethod.POST) 

    public Clients clientRegister(@Named("name") String name, @Named("email") String email, @Named("password") String pass) { 
     OfyService.ofy().factory().begin(); 

     String newApiKey = ""; 
     boolean apiKeyRegistered = false; 

     do{ 
      newApiKey = generateApiKey(); 

      //check whether the apikey is registered 
      Query<Clients> q = OfyService.ofy().load().type(Clients.class).filter("clientApiKey", newApiKey); 
      for (Clients car: q) { 
       apiKeyRegistered = true; 
      } 
     }while(apiKeyRegistered = true); 

     // register 
     Clients client = new Clients(newApiKey, name, email, pass); 
     OfyService.ofy().save().entity(client).now(); 

     return client; 
    } 

    private String generateApiKey(){ 
     String apiKey = ""; 
     String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321"; 
     int charsLength = chars.length(); 

     for (int i = 0; i < 25; i++){ 
      int beginIndex = rand.nextInt(24); 
      apiKey += chars.substring(beginIndex, beginIndex+1); 
     } 

     return apiKey; 
    } 

} 

最後,這裏是我的javascript代碼:

/* 
* http://stackoverflow.com/questions/18260815/use-gapi-client-javascript-to-execute-my-custom-google-api 
* https://developers.google.com/appengine/docs/java/endpoints/consume_js 
* https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientload 
* 
*/ 

/** 
* After the client library has loaded, this init() function is called. 
* The init() function loads the helloworldendpoints API. 
*/ 

function init() { 

    // You need to pass the root path when you load your API 
    // otherwise calls to execute the API run into a problem 

    // rootpath will evaulate to either of these, depending on where the app is running: 
    // //localhost:8080/_ah/api 
    // //your-app-id/_ah/api 

    var rootpath = "//" + window.location.host + "/_ah/api"; 

    // Load the helloworldendpoints API 
    // If loading completes successfully, call loadCallback function 
    gapi.client.load('helloworldendpoints', 'v1', loadCallback, rootpath); 
} 

/* 
* When helloworldendpoints API has loaded, this callback is called. 
* 
* We need to wait until the helloworldendpoints API has loaded to 
* enable the actions for the buttons in index.html, 
* because the buttons call functions in the helloworldendpoints API 
*/ 
function loadCallback() { 
    // Enable the button actions 
    enableButtons(); 
} 

function enableButtons() { 
    // Set the onclick action for the first button 
    btn = document.getElementById("btnLogin"); 
    btn.onclick= function(){login();}; 

    // Update the button label now that the button is active 
    btn.value="Client Login"; 

    // Set the onclick action for the second button 
    btn = document.getElementById("btnRegister"); 
    btn.onclick=function(){register();}; 

    // Update the button label now that the button is active 
    btn.value="Client Register"; 

} 


/* 
* Execute a request to the login() endpoints function 
*/ 
function login() { 
    // Construct the request for the clientLogin() function 
    //var request = gapi.client.helloworldendpoints.clientLogin(); 

    // Execute the request. 
    // On success, pass the response to sayHelloCallback() 
    //request.execute(sayHelloCallback); 
} 

/* 
* Execute a request to the register() endpoints function 
*/ 
function register() { 
    // Construct the request for the clientRegister() function 
    var name = document.getElementById("txtRegisterName").value; 
    var email = document.getElementById("txtRegisterEmail").value; 
    var pass = document.getElementById("txtRegisterPassword").value; 

    alert("requesting"); 

    var request = gapi.client.helloworldendpoints.clientRegister({"name" : name, "email" : email, "password" : pass}); 

    alert("requested"); 
    // Execute the request. 
    // On success, pass the response to sayHelloCallback() 
    request.execute(sayHelloCallback); 
} 



// Process the JSON response 
// In this case, just show an alert dialog box 
// displaying the value of the message field in the response 
function sayHelloCallback (response) { 
    alert(response.toString()); 
} 

而且,JavaScript中的sayHelloCallback函數只在alert中顯示「false」。

我哪裏錯了? 請幫我在數據存儲中創建實體。 謝謝:)

回答