3

我正在从 android 项目访问端点。App Engine 项目已成功部署。

这是我的安卓代码:

Usermasterendpoint.Builder endpointBuilder = new 

Usermasterendpoint.Builder(AndroidHttp.newCompatibleTransport(),

           new JacksonFactory(),

           new HttpRequestInitializer() {

           public void initialize(HttpRequest httpRequest) { }

           });


            Usermasterendpoint endpoint = CloudEndpointUtils.updateBuilder(

        endpointBuilder).build();


            System.out.println("End of Asynk Task is...................///////////");

        try {

           UserMaster userManager = new UserMaster();//.setDescription("Note Description");

           String userID = new Date().toString();

           userManager.setUserID(userID);

           userManager.setEmail(Email);

           userManager.setUsername(username);

           userManager.setPassword(password);

           UserMaster UsermanagerResult = endpoint.insertUserMaster(userManager).execute();

这是我的 UserMaster 实体类。我试图从上面的android代码中使用它。

package com.jay.myapp.dao;


//********************************************************************************************************* 
//Author - Jay
//Description - This class is Data Access Object (DAO) for UserMasterDao entity
//        - Class is basically business class and mostly related to GAE/web application
//*********************************************************************************************************

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.logging.Logger;

import com.google.appengine.api.users.User;
import com.jagdish.mydprapp.model.UserMaster;
import com.jagdish.mydprapp.model.EMF;

public enum UserMasterDao {

    INSTANCE;

    private static final Logger log = Logger.getLogger(UserMasterDao.class.getName());

//*****************************************************************************************************
//getUserByUsermasterID - This method will return List of users or user by getUserByUsermasterID
//*****************************************************************************************************

@SuppressWarnings("unchecked") //Suppress warning for getResultList()
public List<UserMaster> getUserByUsermasterID(Long usermasterID) {

log.info("Inside getUserByUsermasterID");  
EntityManager em = EMF.get().createEntityManager();
// Read the existing entries
Query q = em.createQuery("select t from UserMaster t where t.usermasterID = :usermasterID");
q.setParameter("usermasterID", usermasterID);
List<UserMaster> userlist = q.getResultList();
log.info("returning userlist");

return userlist;

}

//*****************************************************************************************************
//addUser - This method will add User to 
//*****************************************************************************************************

public void addUser(String userID, String employeeID, String mediamasterID, User user,
          String username, String password, String passwordhintquestion, String passwordhintanswer,
          String firstname, String lastname, String email, Long primaryphone, Long secondaryphone,
          Long primarymobile, Long secondarymobile, String gender, Long birthdate,
          String registrationID, String paypalID, String creditcardname, String creditcardnumber,
          String creditcardexpiration, String creditcardcvccode, String creditcardtype,
          boolean activeflag, String updatedby, String dateadded, String dateupdated, String comments) {

      synchronized (this) {
          log.info("Inside addUser");
          EntityManager em = EMF.get().createEntityManager();

          //get current date time
          Date date = new Date();
          SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
          String formattedDate = sdf.format(date);


          //using user1 object variable instead of user - conflict issue with constructor 
          UserMaster user1 = new UserMaster(userID,  employeeID,  mediamasterID, user,
                  username,  password,  passwordhintquestion,  passwordhintanswer,
                  firstname,  lastname,  email,  primaryphone,  secondaryphone,
                  primarymobile,  secondarymobile,  gender,  birthdate,
                  registrationID,  paypalID,  creditcardname,  creditcardnumber,
                  creditcardexpiration,  creditcardcvccode,  creditcardtype,
                  activeflag,  updatedby,  formattedDate,  formattedDate,  comments);

          em.persist(user1);
          em.close();
          log.info("User added to UserMaster" + user1);
      }
}


//*****************************************************************************************************
// createDefaultUser
//*****************************************************************************************************

public void createDefaultUser(User user, UserMaster appUser, GenericDao<UserMaster> userDao) {

    //get current date time
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
    String formattedDate = sdf.format(date);

    log.info("current date time " + formattedDate);

    appUser = new UserMaster(user.getUserId(),"na","na",user, "na","na",
            "na", "na", user.getNickname(), "na", user.getEmail(), 0L, 0L,
          0L, 0L, "na", 0L, "na", user.getEmail(), "na", "na",
          "na", "na", "na", true, user.getUserId(), formattedDate, formattedDate, "na");

    userDao.put(appUser);

}




//*****************************************************************************************************
//removeUser - This method will remove User from table
//*****************************************************************************************************

public void removeUser(Long usermasterID) {
EntityManager em = EMF.get().createEntityManager();
try {
    log.info("Inside removeUser");
    UserMaster user = em.find(UserMaster.class, usermasterID);
    em.remove(user);
    log.info("User entry deleted");
} finally {
 em.close();
}
}



} //end of class

检查我得到的错误:

09-04 14:42:55.946: W/System.err(1603): 

com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable

09-04 14:42:55.946: W/System.err(1603): {

09-04 14:42:55.946: W/System.err(1603):   "code" : 503,

09-04 14:42:55.946: W/System.err(1603):   "errors" : [ {

09-04 14:42:55.946: W/System.err(1603):     "domain" : "global",
09-04 14:42:55.956: W/System.err(1603):     "message" : "java.lang.NullPointerException",
09-04 14:42:55.956: W/System.err(1603):     "reason" : "backendError"
09-04 14:42:55.956: W/System.err(1603):   } ],
09-04 14:42:55.956: W/System.err(1603):   "message" : "java.lang.NullPointerException"
09-04 14:42:55.956: W/System.err(1603): }
09-04 14:42:55.956: W/System.err(1603):     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:111)
09-04 14:42:55.956: W/System.err(1603):     at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:38)
09-04 14:42:55.986: W/System.err(1603):     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
09-04 14:42:55.986: W/System.err(1603):     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1042)
09-04 14:42:55.986: W/System.err(1603):     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
09-04 14:42:55.986: W/System.err(1603):     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
09-04 14:42:55.986: W/System.err(1603):     at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
09-04 14:42:55.996: W/System.err(1603):     at com.jagdish.mydprapp.model.UserManagerAccessorClass.doInBackground(UserManagerAccessorClass.java:57)
09-04 14:42:55.996: W/System.err(1603):     at com.jagdish.mydprapp.model.UserManagerAccessorClass.doInBackground(UserManagerAccessorClass.java:1)
09-04 14:42:56.007: W/System.err(1603):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-04 14:42:56.026: W/System.err(1603):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-04 14:42:56.026: W/System.err(1603):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-04 14:42:56.037: W/System.err(1603):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-04 14:42:56.037: W/System.err(1603):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-04 14:42:56.046: W/System.err(1603):     at java.lang.Thread.run(Thread.java:856)

请帮我解决这个问题。提前致谢!!!

4

1 回答 1

3

我得到了你必须检查天气自动生成的 id 是否为空的答案。这是第一次给我null。只是我必须在 App 引擎项目的端点类中添加一个 if 语句。在端点类中,您将获得

containsYourEntityClass(YourEntityClass xyz) .

你必须添加

if(YourEntityClass.getxxxID() == null)
                return false;

整个代码是

private boolean containsYourEntityClass(YourEntityClass xyz) {
        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {
            if(xyz.getxxxID() == null)
                return false;
            YourEntityClass item = mgr.find(YourEntityClass.class,
                    xyz.getxxxID());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

于 2013-10-16T08:49:17.277 回答