gpt4 book ai didi

java - 为什么我在使用 Google App Engile 的端点插入数据时在 Android 中出现错误?

转载 作者:太空狗 更新时间:2023-10-29 12:45:42 24 4
gpt4 key购买 nike

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

这是我的 Android 代码:

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)

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

最佳答案

我得到的答案是您必须检查天气自动生成的 ID 是否为空。这是第一次给我 null 。只是我必须在 App Engine 项目的端点类中添加一个 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;
}

关于java - 为什么我在使用 Google App Engile 的端点插入数据时在 Android 中出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18618621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com