Android Studio appengine endpoint does not include builder

血红的双手。 提交于 2019-12-11 03:21:51

问题


In my AndroidStudio project I have two modules: app, which is the android module and api which is the app-engine module. The App-Engine module api was generated by creating an Endpoints App Engine Module in AndroidStudio.

I have an Endpoint class generated from an objectify annotated class called 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;
    }
}

Later on I generated an Endpoint called CommentEndpoint from the class by hitting Tools > Google Cloud Tools > Generate Endpoint

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

When I try to create an AsyncTask in my app module do that the Android Application can work with the App-Engine Backend I need the Endpoints member class Builder which should be instantiated like this

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

But the IDE tells me there is no such thing as CommentEndpoint.Builder. I googled this question but to no avail, since Android Studio works with two modules in the same project now and not in two separate projects for the app-engine and android application. Have I missed something?


回答1:


I just recently installed AS 0.8.9 and began to work on a project involving GAE. I had this same problem with 'builder not identified'.

What fixed it for me was deleting the "in your case":

import com.example.pontuse.api.CommentEndpoint;

and re importing it which changed it to

import com.example.pontuse.api.commentEndpoint.CommentEndpoint;

this must be a bug when adding the dependency. Hope this helps.




回答2:


Maybe you forgot to add the dependencies to build.gradle of your app module, as mentioned here. Add the following under 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.+')

Then you should import: com.example.pontuse.api.commentEndpoint.CommentEndpoint instead of com.example.pontuse.api.CommentEndpoint




回答3:


I think it could be because of endpointn client library. Have you used the getClientLibsOnBuild in your gradle file? Just an Fyi - first time around it takes a while to download upon gradle sync.

appengine{ endpoints {

  // downloads client libs
getClientLibsOnBuild = true

} }

You could also manually build only client lib from your project folder as:

gradlew api:appengineEndpointsGetClientLibs




回答4:


I got the same problem in Android Studio. I generated my Endpoint class from my entity java bean but when creating the AsyncTask, now way to get the Builder.

With a Comment java bean like yours (and corresponding CommentEndpoint), I realized that the Builder is not depending on the CommentEndPoint but on a auto-generated CommentApi class.

In other words, I had to add these two imports in the AsyncTask class:

import com.example.pontuse.api.commentApi.CommentApi;

import com.example.pontuse.api.commentApi.model.Comment;



来源:https://stackoverflow.com/questions/25748095/android-studio-appengine-endpoint-does-not-include-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!