我的api是建立在app engine端点平台上的。在我将生成的库附加到我的android客户端之后,使用它并构建一个应用程序,它总是说找不到我的api或api$builder(在运行时)。在开发代码或编译时,它是清晰可见和可访问的。
我尝试了两种不同的方法:
-附加maven appengine生成的.jar:endpoints_get_client_lib goal
-附加由相同目标生成的这些类的源
这两种方法都使类在运行时可见并且仍然可用存在一个异常noclassdeffound(当我实际调用它时)和一个错误,即dalvikvm找不到该类。
我已选中“订单/导出”选项卡中的引用库。尽管它是在最新的位置(与任何其他尝试)。我还检查了android 5.0库以及android私有库(未选中android依赖项和maven依赖项-也未选中maven的)。
值得一提的是,当我使用gae插件(没有maven)在eclipse中创建测试项目时,它工作得很好。我想不出问题在哪里。
我可以提供任何你想解决这个问题的信息。只需询问评论,我就可以更新所需的信息。
我在android应用程序mainactivity中的代码的一部分:
protected TestUser callApi() throws IOException
{
Api.Builder api = new Api.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);
return api.build().hello("ANY").execute();
}
我的API代码:
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.config.Named;
import com.test.user.Contact;
import com.test.user.TestUser;
import com.test.user.status.Status;
import com.test.utils.Utils;
/** An endpoint class we are exposing */
@Api(
name = "api",
version = "v1",
description = "My Own Api",
namespace = @ApiNamespace(
ownerDomain = "com.test.server",
ownerName = "com.test.server",
packagePath = ""),
scopes = { Utils.EMAIL_SCOPE }
)
public class ApiService
{
/**
* Api Method for Testing purposes.
*
* @param name
* @return
*/
@ApiMethod(name = "hello")
public TestUser hello(@Named("name") String name)
{
TestUser response = new TestUser();
response.setId(123);
response.setName(name);
return response;
}
}
我的android项目的pom:
<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<appengine.app.version>2</appengine.app.version>
<appengine.target.version>1.9.14</appengine.target.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.target.version}</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-endpoints</artifactId>
<version>${appengine.target.version}</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-android</artifactId>
<version>1.19.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.9.0-rc.1</version>
<configuration>
<sdk>
<platform>21</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
我的API应用程序的POM:
<pluginRepositories>
<pluginRepository>
<id>google-staging</id>
<name>Google Staging</name>
<url>https://oss.sonatype.org/content/repositories/comgoogleappengine-1004/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<appengine.app.version>1</appengine.app.version>
<appengine.target.version>1.9.14</appengine.target.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.target.version}</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-endpoints</artifactId>
<version>1.9.14</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo-api</artifactId>
<version>3.1-rc1</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.target.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.target.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<outputDirectory>target/${project.artifactId}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
<!-- in order to interpolate version from pom into appengine-web.xml -->
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.9.14</version>
<configuration>
<enableJarClasses>false</enableJarClasses>
<port>8888</port>
<address>0.0.0.0</address>
</configuration>
</plugin>
</plugins>
</build>
编辑14:43 2014年11月1日:
Android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.client"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/abc_max_action_buttons" />
<activity
android:name="MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最佳答案
此错误通常意味着在服务器上找不到所需的jar。您在eclipse中没有看到错误,因为您确实将所有必需的jar添加到了类路径中。
在eclipse中,选择您的项目并单击problems选项卡。看看有没有警告说…将无法在服务器上使用…。右键单击此警告并选择“复制”选项。
注意,另一种可能是在该文件夹中有多个所需jar的副本,在这种情况下,应该删除所有副本,添加正确的jar,并清理项目。