android - 在 Google Cloud Endpoints(App Engine 后端)中执行 "get"并在 Android 应用程序上进行 objectify 时出现空指针异常

标签 android google-app-engine google-cloud-endpoints objectify

我按照here给出的教程进行操作开发我的代码。 由于整个代码在上面的链接以及 here 中给出。为了避免困惑,我不会在这里全部输入。

在应用程序引擎应用程序上,我有一个这样的类:

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

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

public Quote() {}

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

由于我从上面的类自动生成了 Endpoints 类,因此它看起来(部分)像这样(与示例中显示的相反)

@Api(
        name = "quoteApi",
        version = "v1",
        resource = "quote",
        namespace = @ApiNamespace(
                ownerDomain = "QuoteApi.com",
                ownerName = "QuoteApi.com",
                packagePath = ""
        )
)
public class QuoteEndpoint {

    private static final Logger logger = Logger.getLogger(QuoteEndpoint.class.getName());

    private static final int DEFAULT_LIST_LIMIT = 20;

    static {
        // Typically you would register this inside an OfyServive wrapper. See: https://code.google.com/p/objectify-appengine/wiki/BestPractices
        ObjectifyService.register(Quote.class);
    }

    /**
     * Returns the {@link Quote} with the corresponding ID.
     *
     * @param id the ID of the entity to be retrieved
     * @return the entity with the corresponding ID
     * @throws NotFoundException if there is no {@code Quote} with the provided ID.
     */
    @ApiMethod(
            name = "get",
            path = "quote/{id}",
            httpMethod = ApiMethod.HttpMethod.GET)
    public Quote get(@Named("id") Long id) throws NotFoundException {
        logger.info("Getting Quote with ID: " + id);
        Quote quote = ofy().load().type(Quote.class).id(id).now();
        if (quote == null) {
            throw new NotFoundException("Could not find Quote with ID: " + id);
        }
        return quote;
    }
}

现在在我的 Android 应用程序中,我执行此操作。这在 AsyncTask 中,doinbackground

    try {
        Long id = Long.getLong("9649299534313129");
        Quote q1= endpointBldr.get(id).execute();

    }catch (IOException e){
        return e.getMessage();
    }

我应该提到,我要求获取的“id”是准确的,并且存在于数据存储中。 此外,“列表”、“插入”和“删除”ApiMethods 工作得很好。只是这个“得到”给我带来了麻烦。 所有这些都是自动生成的方法。

最后我得到的停止我的应用程序的错误是:

/com.blah.QuoteApi E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.blah.QuoteApi, PID: 16820
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

Caused by: java.lang.NullPointerException: Required parameter id must be specified.
            at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:208)
            at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:140)
            at com.blah.QuoteApi$Get.<init>(Quote.java:151)
            at com.blah.QuoteApi.QuoteApi.get(QuoteApi.java:129)
            at com.blah.QuoteApi.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:72)
            at com.blah.QuoteApi.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:22)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

有人可以帮我解决这个问题吗?不确定它在哪里获取空指针,我给出分配的 Long id,作为 get 的参数。

另外,应该指出的是,ApiMethod 在在线 Google Api Explorer (AppSpot) 中正常工作

最佳答案

我必须承认,这是一个最愚蠢的错误。 Long.getLong 不适合使用。 Long.parseLong 应该是那个。 在这种情况下,getLong 显然会给出 null 。

关于android - 在 Google Cloud Endpoints(App Engine 后端)中执行 "get"并在 Android 应用程序上进行 objectify 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844974/

相关文章:

javascript - Firebase 断开连接

android - 父 fragment 不显示子 ViewPager

google-app-engine - 如何强制 Google 云端点仅在整个 API 或特定方法上使用 https?

android - Android 应用程序 Azure 依赖项 gradle 构建时出错

Android游戏循环,如何控制速度

java - 适用于 Java 的博客软件/平台

python代码问题

Python、gae、ndb——以一种方式获取所有键

google-maps - 恢复已删除的 Google 公共(public) API key

google-app-engine - 生成 Cloud Endpoint 时遇到错误且未完成