在我的AndroidStudio项目中,我有两个模块:app,即android模块,api是app-engine模块。 App-Engine模块api是通过在AndroidStudio中创建端点应用引擎模块生成的。
我有一个Endview类,它是从一个名为Comment的客观化注释类生成的。
package com.example.pontuse.api;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
/**
* Created by pontuse on 2014-09-08.
*/
@Entity
public class Comment {
@Id
Long id;
String who;
String txt;
public Comment() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
}
后来我通过点击Tools>从类中生成了一个名为CommentEndpoint的端点。 Google云端工具>生成端点
package com.example.pontuse.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.Nullable;
import com.google.api.server.spi.response.CollectionResponse;
import com.google.api.server.spi.response.ConflictException;
import com.google.api.server.spi.response.NotFoundException;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.cmd.Query;
import static com.example.pontuse.api.OfyService.ofy;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Named;
import javax.xml.ws.Endpoint;
/** An endpoint class we are exposing */
@Api(name = "commentEndpoint", version = "v1", namespace = @ApiNamespace(ownerDomain = "api.pontuse.example.com", ownerName = "api.pontuse.example.com", packagePath=""))
public class CommentEndpoint{
// Make sure to add this endpoint to your web.xml file if this is a web application.
private static final Logger LOG = Logger.getLogger(CommentEndpoint.class.getName());
public CommentEndpoint(){
}
@ApiMethod(name = "listComment")
public CollectionResponse<Comment> listComment(@Nullable @Named("cursor") String cursorString,
@Nullable @Named("count") Integer count) {
Query<Comment> query = ofy().load().type(Comment.class);
if (count != null) query.limit(count);
if (cursorString != null && cursorString != "") {
query = query.startAt(Cursor.fromWebSafeString(cursorString));
}
LOG.info("Calling listComment method");
List<Comment> records = new ArrayList<Comment>();
QueryResultIterator<Comment> iterator = query.iterator();
int num = 0;
while (iterator.hasNext()) {
records.add(iterator.next());
if (count != null) {
num++;
if (num == count) break;
}
}
if (cursorString != null && cursorString != "") {
Cursor cursor = iterator.getCursor();
if (cursor != null) {
cursorString = cursor.toWebSafeString();
}
}
return CollectionResponse.<Comment>builder().setItems(records).setNextPageToken(cursorString).build();
}
/**
* This inserts a new <code>Comment</code> object.
* @param comment The object to be added.
* @return The object to be added.
*/
@ApiMethod(name = "insertComment")
public Comment insertComment(Comment comment) throws ConflictException{
if(comment.getId() != null)
if(findRecord(comment.getId()) != null) throw new ConflictException("Object already exists");
LOG.info("Calling insertComment method");
ofy().save().entity(comment).now();
return comment;
}
@ApiMethod (name = "updateComment")
public Comment updateComment(Comment comment) throws NotFoundException{
if(findRecord(comment.getId()) == null) throw new NotFoundException("Object does not exist");
ofy().save().entity(comment).now();
return comment;
}
@ApiMethod (name = "removeComment")
public void removeComment(@Named ("id") Long id) throws NotFoundException{
Comment record = findRecord(id);
if(record == null) throw new NotFoundException("Comment record does not exist!");
LOG.info("Calling removeComment method");
ofy().delete().entity(record).now();
}
private Comment findRecord(Long id){
return ofy().load().type(Comment.class).id(id).now();
}
}
当我尝试在我的app模块中创建AsyncTask时,Android应用程序可以使用App-Engine后端我需要Endpoints成员类Builder,它应该像这样实例化
CommentEndpoint.Builder builder = new CommentEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
但IDE告诉我没有CommentEndpoint.Builder这样的东西。我用Google搜索this question但无济于事,因为Android Studio现在在同一项目中使用两个模块,而不是在app-engine和android应用程序的两个单独项目中。 我错过了什么吗?
答案 0 :(得分:2)
也许您忘了将依赖项添加到app模块的build.gradle中,如上所述here。 在依赖项下添加以下内容:
// BEGIN Google APIs
// Play Services will validate the application prior to allowing OAuth2 access.
compile(group: 'com.google.android.gms', name: 'play-services', version: '3.2.+')
// The following lines implement maven imports as defined at:
// https://code.google.com/p/google-api-java-client/wiki/Setup
// Add the Google API client library.
compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.17.0-rc') {
// Exclude artifacts that the Android SDK/Runtime provides.
exclude(group: 'xpp3', module: 'xpp3')
exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
exclude(group: 'junit', module: 'junit')
exclude(group: 'com.google.android', module: 'android')
}
// Add the Android extensions for the Google API client library.
// This will automatically include play services as long as you have download that library
// from the Android SDK manager.
// Add the Android extensions for the Google API client library.
compile(group: 'com.google.api-client', name: 'google-api-client-android',
version: '1.17.0-rc')
{
// Exclude play services, since we're not using this yet.
exclude(group: 'com.google.android.google-play-services', module: 'google-play-services')
}
// END Google APIs
// The following client libraries make HTTP/JSON on Android easier.
// Android extensions for Google HTTP Client.
compile(group: 'com.google.http-client', name: 'google-http-client-android',
version: '1.17.0-rc') {
exclude(group: 'com.google.android', module: 'android')
}
// This is used by the Google HTTP client library.
compile(group: 'com.google.guava', name: 'guava', version: '14.0.+')
然后您应该导入:com.example.pontuse.api.commentEndpoint.CommentEndpoint
而不是com.example.pontuse.api.CommentEndpoint
答案 1 :(得分:1)
我刚刚安装了AS 0.8.9并开始研究涉及GAE的项目。 “生成器未识别”时遇到了同样的问题。
为我修复的是删除“在你的情况下”:
import com.example.pontuse.api.CommentEndpoint;
并重新导入它,将其更改为
import com.example.pontuse.api.commentEndpoint.CommentEndpoint;
添加依赖项时,这必定是一个错误。 希望这会有所帮助。
答案 2 :(得分:1)
我认为这可能是因为endpointn客户端库。您是否在gradle文件中使用了getClientLibsOnBuild?只是一个Fyi - 第一次在gradle同步下下载需要一段时间。
应用服务引擎{ 终点{
// downloads client libs
getClientLibsOnBuild = true
} }
您也可以从项目文件夹中手动构建客户端lib:
gradlew api:appengineEndpointsGetClientLibs
答案 3 :(得分:1)
我在Android Studio中遇到了同样的问题。我从我的实体java bean生成了我的Endpoint类,但在创建AsyncTask
时,现在可以获得Builder了。
使用像您这样的Comment
java bean(和相应的CommentEndpoint),我意识到Builder不依赖于CommentEndPoint,而是依赖于自动生成的CommentApi类。
换句话说,我必须在AsyncTask类中添加这两个导入:
import com.example.pontuse.api.commentApi.CommentApi;
import com.example.pontuse.api.commentApi.model.Comment;