实际和形式参数列表的长度不同 - EndpointAsyncTask - Android

Actual and formal argument lists differ in length - EndpointAsyncTask - Android

我将这个 EndPointAsyncTask.class 放入我的 app 模块中:

package com.kkoci.shairlook;

/**
* Created by kristian on 02/07/2015.
*/
import android.content.Context;
import android.os.AsyncTask;
import android.util.Pair;
import android.widget.Toast;

import com.appspot.shairlook1.userEndpoint.UserEndpoint;
import com.appspot.shairlook1.userEndpoint.model.User;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

class EndpointsAsyncTask extends AsyncTask<Void, Void, List<User>> {
private static UserEndpoint myApiService = null;
private Context context;

EndpointsAsyncTask(Context context) {
    this.context = context;
}

@Override
protected List<User> doInBackground(Void... params) {
    if(myApiService == null) { // Only do this once
        UserEndpoint.Builder builder = new UserEndpoint().Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
                .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                    @Override
                    public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                        abstractGoogleClientRequest.setDisableGZipContent(true);
                    }
                });
// end options for devappserver

        myApiService = builder.build();
    }

    try {
        return myApiService.listUser().execute().getItems();
    } catch (IOException e) {
        return Collections.EMPTY_LIST;
    }
}

@Override
protected void onPostExecute(List<User> result) {
    for (User q : result) {
        Toast.makeText(context, q.getWho() + " : " + q.getWhat(), Toast.LENGTH_LONG).show();
    }
}
}

这从 User class 从 google 应用引擎消耗到我的后端模块:

package com.kkoci.shairlook.backend;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;


/**
* Created by kristian on 01/07/2015.
*/

@Entity
public class User {
@Id
Long id;
String who;
String what;

public User() {}

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 getWhat() {
    return what;
}

public void setWhat(String what) {
    this.what = what;
}
}

这是我的 UserEndPoint class:

package com.kkoci.shairlook.backend;

import com.kkoci.shairlook.backend.User;
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.kkoci.shairlook.backend.OfyService.ofy;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.inject.Named;

/**
* Created by kristian on 01/07/2015.
*/

@Api(name = "userEndpoint", version = "v1", namespace =     @ApiNamespace(ownerDomain = "shairlook1.appspot.com", ownerName = "shairlook1.appspot.com", packagePath=""))
public class UserEndPoint {

// Make sure to add this endpoint to your web.xml file if this is a web application.

public UserEndPoint() {

}

/**
 * Return a collection of users
 *
 * @param count The number of users
 * @return a list of Users
 */
@ApiMethod(name = "listUser")
public CollectionResponse<User> listUser(@Nullable @Named("cursor")   String cursorString,
                                           @Nullable @Named("count") Integer count) {

    Query<User> query = ofy().load().type(User.class);
    if (count != null) query.limit(count);
    if (cursorString != null && cursorString != "") {
        query = query.startAt(Cursor.fromWebSafeString(cursorString));
    }

    List<User> records = new ArrayList<User>();
    QueryResultIterator<User> iterator = query.iterator();
    int num = 0;
    while (iterator.hasNext()) {
        records.add(iterator.next());
        if (count != null) {
            num++;
            if (num == count) break;
        }
    }

//Find the next cursor
    if (cursorString != null && cursorString != "") {
        Cursor cursor = iterator.getCursor();
        if (cursor != null) {
            cursorString = cursor.toWebSafeString();
        }
    }
    return CollectionResponse.<User>builder().setItems(records).setNextPageToken(cursorString).build();
}

/**
 * This inserts a new <code>User</code> object.
 * @param user The object to be added.
 * @return The object to be added.
 */
@ApiMethod(name = "insertUser")
public User insertUser(User user) throws ConflictException {
//If if is not null, then check if it exists. If yes, throw an Exception
//that it is already present
    if (user.getId() != null) {
        if (findRecord(user.getId()) != null) {
            throw new ConflictException("Object already exists");
        }
    }
//Since our @Id field is a Long, Objectify will generate a unique value for us
//when we use put
    ofy().save().entity(user).now();
    return user;
}

/**
 * This updates an existing <code>User</code> object.
 * @param user The object to be added.
 * @return The object to be updated.
 */
@ApiMethod(name = "updateUser")
public User updateUser(User user)throws NotFoundException {
    if (findRecord(user.getId()) == null) {
        throw new NotFoundException("User Record does not exist");
    }
    ofy().save().entity(user).now();
    return user;
}

/**
 * This deletes an existing <code>User</code> object.
 * @param id The id of the object to be deleted.
 */
@ApiMethod(name = "removeUser")
public void removeUser(@Named("id") Long id) throws NotFoundException {
    User record = findRecord(id);
    if(record == null) {
        throw new NotFoundException("User Record does not exist");
    }
    ofy().delete().entity(record).now();
}

//Private method to retrieve a <code>User</code> record
private User findRecord(Long id) {
    return ofy().load().type(User.class).id(id).now();
//or return ofy().load().type(User.class).filter("id",id).first.now();
}

}

我正在使用 GAE 版本 1.9.18... 我不知道这是否是版本问题,但在 EndpointAsyncTask 进入这一行:

UserEndpoint.Builder builder = new UserEndpoint().Builder(AndroidHttp.newCompatibleTransport(),

EndpointAsyncTask 不断抛出此错误:

    Error:(33, 44) error: no suitable constructor found for UserEndpoint()
constructor UserEndpoint.UserEndpoint(Builder) is not applicable
(actual and formal argument lists differ in length)
constructor UserEndpoint.UserEndpoint(HttpTransport,JsonFactory,HttpRequestInitializer) is not applicable
(actual and formal argument lists differ in length)

我不知道它可能是什么,这是我的 backend GAE gradle 配置文件:

dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.18'
compile 'com.google.appengine:appengine-endpoints:1.9.18'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.18'
compile 'com.googlecode.objectify:objectify:5.0.3'
compile 'javax.servlet:servlet-api:2.5'
}

有什么想法吗?

提前致谢!

您的 Builder 构造函数。 您正在创建一个新的 Builder 实例作为

UserEndpoint.Builder builder = new UserEndpoint().Builder(AndroidHttp.newCompatibleTransport()...)

但需要被称为

UserEndpoint.Builder builder = new UserEndpoint.Builder(AndroidHttp.newCompatibleTransport()...)