gpt4 book ai didi

android - 在 Google Cloud Endpoints(App Engine 后端)中执行 "get"并在 Android App 上进行对象化时出现空指针异常

转载 作者:行者123 更新时间:2023-11-29 00:13:02 24 4
gpt4 key购买 nike

我遵循了给定的教程 here开发我的代码。由于上面的链接以及 here 给出了整个代码为了避免困惑,我不会在这里全部输入。

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

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 App 上进行对象化时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844974/

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