本文介绍了为什么我收到错误的Andr​​oid同时插入使用谷歌应用程序Engile端点的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Android项目访问端点。该应用程序引擎项目已成功部署。

I am accessing endpoints from android project. The App Engine Project has been deployed successfully.

这是我的Andr​​oid code:

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实体类。而我试图从上面的Andr​​oid code使用。

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)

请帮我在这个问题上理清。在此先感谢!

Please help me to sort out the this issue. Thanks in advance!!!

推荐答案

我把你要检查的天气自动答案生成的ID为空或不是。这是给我空第一次。只是我有if语句添加一个在我的端点类的应用程序引擎项目。在端点类,你会得到

I got the answer you have to check weather auto generated id is null or not. It was giving me null first time. Just i have to add one if statement in my endpoint class in App engine project. in endpoint class you will get

containsYourEntityClass(YourEntityClass xyz) .

你必须添加

You have to add

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

整个code是

The whole code is

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;
    }

这篇关于为什么我收到错误的Andr​​oid同时插入使用谷歌应用程序Engile端点的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 15:09