gpt4 book ai didi

java - Android Studio appengine 端点不包含构建器

转载 作者:太空狗 更新时间:2023-10-29 16:37:10 25 4
gpt4 key购买 nike

在我的 AndroidStudio 项目中,我有两个模块:app,即 android 模块和 api,即应用程序引擎模块。 App-Engine 模块 api 是通过在 AndroidStudio 中创建 Endpoints App Engine 模块生成的。

我有一个端点类,它是从一个名为 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;
}
}

稍后,我通过点击“工具”>“Google Cloud 工具”>“生成端点”从类中生成了一个名为 CommentEndpoint 的端点

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();
}
}

当我尝试在我的应用程序模块中创建一个 AsyncTask 时,Android 应用程序可以与 App-Engine 后端一起工作,我需要 Endpoints 成员类 Builder,它应该像这样实例化

CommentEndpoint.Builder builder = new CommentEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)

但是 IDE 告诉我没有 CommentEndpoint.Builder 这样的东西。我用谷歌搜索了 this question但无济于事,因为 Android Studio 现在在同一个项目中使用两个模块,而不是在应用程序引擎和 android 应用程序的两个单独项目中。我错过了什么吗?

最佳答案

也许您忘记将依赖项添加到应用程序模块的 build.gradle 中,如前所述 here .在 dependencies 下添加如下内容:

// 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

关于java - Android Studio appengine 端点不包含构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25748095/

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