我正在尝试遵循 https://developers.google.com/eclipse/docs/endpoints-addentities 上的示例教程,但我一直在思考如何在 Eclipse 中生成
GameEndpoint.Builder
类。
按照描述生成云端点后,我创建了一个
GameEndpoint
类,但没有 GameEndpoint.Builder
类。很明显我有这个错误
GameEndpoint.Builder 无法解析为类型
此时我很困惑。如何在 Eclipse 中生成
GameEndpoint.Builder
类,或者什么会阻止它?
代码
public class NewGameTask extends AsyncTask<Context, Integer, Long> {
protected Long doInBackground(Context... contexts) {
GameEndpoint.Builder endpointBuilder = new GameEndpoint.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) {
}
});
GameEndpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
Game game = new Game();
game.setStart(Calendar.getInstance());
Game result = endpoint.insertGame(game);
} catch (IOException e) {
e.printStackTrace();
}
return (long) 0;
}
}
在观看 Google I/O 2013 的视频后,我发现了我的问题,该视频使用的是 Android Studio,但它大部分与 Eclipse 相同。
我在遵循https://developers.google.com/eclipse/docs/endpoints-addentities时犯的错误是,您需要将实体类放入MyApp-AppEngine项目而不是您的MyApp项目中。
这就是混乱的根源。如果它对将来的人有帮助,这里是我所做的一个简短的细分。
注意这个答案是基于Android Studio的,但我确信它与Eclipse几乎相同。
我也遇到了这个问题,但后来找到了原因。原来我导入的是我生成的
Endpoint
类,而不是端点 Api 包。让我说清楚。当您将端点模块添加到项目中时,您将在端点包中获得 MyBean
和 MyEndpoint
类。如果您查看将客户端连接到后端的指南,则EndpointsAsyncTask
类使用:
private static MyApi myApiService = null;
注意它如何使用
MyApi
而不是 MyBean
现在我想知道它从哪里得到的,但我只需要看看我的后端库:
标记为
1
的库是当您按照前面提到的指南操作时首先添加到项目中的库。当我添加新类 Student
并自动生成云端点类时,还添加了第二个库。
又长又无聊的故事短篇;您应该导入的是这个库,而不是类。
import com.package-name.backend.studentApi.StudentApi;
然后使用:
private static StudentApi myApiService = null;
...
StudentApi.Builder builder = new StudentApi.Builder(...)
而不是:
import com.package-name.backend.StudentEndpoint;
...
private static StudentEndpoint myApiService = null;
StudentEndpoint.Builder builder = new StudentEndpoint.Builder(...)
我在 Android Studio 中遇到了同样的问题。我从实体 java bean 生成了 Endpoint 类,但在创建 AsyncTask 时,现在可以获取 Builder。
实际上(如果我像你一样使用 Game java bean)Builder 并不依赖于 GameEndPoint 而是依赖于生成的 GameApi 类。
换句话说,我必须在 AsyncTask 类中添加这两个导入:
导入com.examplepackage.backend.gameApi.GameApi; 导入 com.examplepackage.backend.gameApi.model.Game;
而您编写的 Game java bean 和生成的 GameEndpoint 位于包 com.examplepackage.backend 下