按照本教程http://www.appsrox.com/android/tutorials/instachat/7/#20,我尝试为我的项目创建一个GCM聊天应用程序。客户项目运作良好。但是,在Server项目中他们使用GAE并且还使用了JPA / JPO,但是他们在Eclipse中工作过,所以让我混淆了JPA如何在build.gradle中设置必要的Jars文件。
我的服务器和客户端项目没有显示错误,但我得到的结果与教程不同。此外,在appengine.google.com日志中,有以下错误行:
Error for /send
java.lang.NoClassDefFoundError: Could not initialize class com.myapp.chat.backend.model.EMFService
at com.myapp.chat.backend.SendServlet.doPost(SendServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
...
此错误涉及以下几行:
SendServlet.java
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = req.getParameter(ServerConstants.MSG);
String from = req.getParameter(ServerConstants.FROM);
String to = req.getParameter(ServerConstants.TO);
Contact contact = null;
EntityManager em = EMFService.get().createEntityManager();
try {
contact = Contact.find(to, em);
if (contact == null) return;
} finally {
em.close();
}
String regId = contact.getRegId();
Sender sender = new Sender(ServerConstants.API_KEY);
Message message = new Message.Builder()
.addData(ServerConstants.TO, to).addData(ServerConstants.FROM, from).addData(ServerConstants.MSG, msg)
.build();
try {
Result result = sender.send(message, regId, 5);
logger.log(Level.WARNING, "Result: " + result.toString());
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage());
}
}
我的Contact.java文件:
public class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email;
private String regId;
public Contact() {}
public Contact(String email, String regId) {
this.email = email;
this.regId = regId;
}
public static Contact find(String email, EntityManager em) {
Query q = em.createQuery("select c from Contact c where c.email = :email");
q.setParameter("email", email);
List<Contact> result = q.getResultList();
if (!result.isEmpty()) {
return result.get(0);
}
return null;
}
public Long getId() {
return id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRegId() {
return regId;
}
public void setRegId(String regId) {
this.regId = regId;
}
}
我的EMFService.java有以下几行:
public class EMFService {
private static final EntityManagerFactory emfInstance = Persistence.createEntityManagerFactory("transactions-optional");
private EMFService() {
}
public static EntityManagerFactory get() {
return emfInstance;
}
private static EntityManager getEntityManager() {
return EMFService.get().createEntityManager();
}
}
我的build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.18'
}
}
repositories {
jcenter();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.18'
compile 'com.google.appengine:appengine-endpoints:1.9.18'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.18'
compile 'javax.servlet:servlet-api:2.5'
compile files('libs/javax.persistence-2.0.0.jar')
compile files('libs/gcm-server.jar')
compile 'jta:jta:1.0.1b'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'com.google.appengine.orm:datanucleus-appengine:2.1.2'
compile 'org.datanucleus:datanucleus-enhancer:3.1.1'
compile 'javax.jdo:jdo2-api:2.3-eb'
compile 'javax.transaction:jta:1.1'
compile 'org.datanucleus:datanucleus-api-jdo:3.1.3' // WE NEED THIS
compile 'org.ow2.asm:asm:4.0'
compile 'org.datanucleus:datanucleus-api-jpa:3.1.3'
}
appengine {
enhancer {
version = "v2"
api="jdo"
enhanceOnBuild = true
}
downloadSdk = true
appcfg {
oauth2 = true
}
endpoints {
getClientLibsOnBuild = true
getDiscoveryDocsOnBuild = true
}
}
Web.xml中
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>com.myapp.chat.backend.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Unregister</servlet-name>
<servlet-class>com.myapp.chat.backend.UnregisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Unregister</servlet-name>
<url-pattern>/unregister</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Send</servlet-name>
<servlet-class>com.myapp.chat.backend.SendServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Send</servlet-name>
<url-pattern>/send</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>com.myapp.chat.backend.MyEndpoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
的persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
<property name="datanucleus.appengine.datastoreEnableXGTransactions" value="true" />
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true" />
</properties>
</persistence-unit>
因为我使用的是不同的IDE,所以我不确定每个文件的放置位置。但是,当我点击&#34;部署App Engine&#34;一切正常。请有人帮我解决调试问题。谢谢!