【问题标题】:How is it possible an endpoint method returns null when null is never returned?当从不返回 null 时,端点方法怎么可能返回 null?
【发布时间】:2014-04-13 09:02:29
【问题描述】:

此方法属于我在 Android Studio 中使用 Google App Engine 后端的 Android 项目中的端点:

@ApiMethod(name = "getAllUnclaimedLeftovers")
public Collection<Leftover> getAllUnclaimedLeftovers() {
    Collection<Leftover> unclaimedLeftoverOffers = new ArrayList<Leftover>();

    for(Leftover leftover : ofy().load().type(Leftover.class)) {
        if (!leftover.isClaimed() && leftover.isValid()) {
            unclaimedLeftoverOffers.add(leftover);
        }
    }

    return unclaimedLeftoverOffers;
}

如您所见,null 将永远不会被返回。 在这里,您可以通过相应的 gradle 命令生成的客户端库使用之前的后端方法在项目的应用程序端看到一个方法。

public static Collection<Leftover> getAllUnclaimedLeftovers() throws IOException {
    try {
        return Proxy.getEndpoint().getAllUnclaimedLeftovers().execute().getItems();
    } catch (IOException e) {
        Log.d("DataStore_UnclaimedLeftovers_Error", e.getMessage());
        throw e;
    }
}

我的问题是:最后一个方法怎么可能返回 null 对象?

现在我正在检查 null 对象,一切正常,应用程序和后端已部署并正常工作。

方法getAllUnclaimedLeftovers()用于:

private class UnclaimedLeftoversTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            setUnclaimedLeftovers(LeftoverProxy.getAllUnclaimedLeftovers());
            setIsUnclaimedLeftoversSucceeded(true);
            return null;
        } catch (IOException e) {
            setIsUnclaimedLeftoversSucceeded(false);
            return null;
        } catch (NullPointerException e) {
            setIsUnclaimedLeftoversSucceeded(false);
            return null;
        }
    }
}

LeftoverProxy.getAllUnclaimedLeftovers() 引用 null 对象。

查看自动生成的端点库时:

public GetAllUnclaimedLeftovers getAllUnclaimedLeftovers() throws java.io.IOException {    
    GetAllUnclaimedLeftovers result = new GetAllUnclaimedLeftovers();
    initialize(result);
    return result;
}

public class GetAllUnclaimedLeftovers extends EndpointRequest<com.frigoshare.endpoint.model.LeftoverCollection> {

    private static final String REST_PATH = "leftovercollection";


    protected GetAllUnclaimedLeftovers() {
      super(Endpoint.this, "GET", REST_PATH, null,   com.frigoshare.endpoint.model.LeftoverCollection.class);
    }

...
}

超类的构造函数:

public EndpointRequest(
  Endpoint client, String method, String uriTemplate, Object content, Class<T> responseClass) {
    super(
        client,
        method,
        uriTemplate,
        content,
        responseClass);
    }

在 AbstractGoogleClientRequest 中搜索时,我找到了execute()

 /**
   * Sends the metadata request to the server and returns the parsed metadata response.
   *
   * <p>
   * Subclasses may override by calling the super implementation.
   * </p>
   *
   * @return parsed HTTP response
   */
  public T execute() throws IOException {
    HttpResponse response = executeUnparsed();
    if (Void.class.equals(responseClass)) {
      response.ignore();
      return null;
    }
    return response.parseAs(responseClass);
  }

但是这种对 Google 代码的挖掘是没有意义的,这些库必须实现正确的行为。我的意思是,或者你得到一个响应,或者你得到一个失败并抛出一个 IOException,但不是介于两者之间?

【问题讨论】:

  • 我假设您在某处获得了 NullPointerExecption?您确定空指针不是针对其他对象(例如端点本身),而不是无人认领的剩余物吗?
  • @peshkira:我在链中添加了另一个接收空对象的方法。我知道这是 null,因为我无法调用 Collection 上的方法(例如:size())
  • 可能接收到getAllUnclaimedLeftovers方法调用的对象是一个子类的一个实例,它已经覆盖了这个方法(并且可能返回null)。
  • @Seelenvirtuose 我猜客户端库中的包装类返回了一个空值,但是到目前为止我认为自动生成的代码决定自己返回空值是没有意义的。

标签: java android json google-app-engine endpoint


【解决方案1】:

从我的GAE 后端获取空集合时,我收到Null。 我在尝试将空集合推送到我的 GAE 后端时收到 jsonMappingException。 所以我猜JSON(或Gradle 插件Android Studio)无法处理两个方向的空集合。

由于这个项目是针对我的课程的,我也看到其他学生的项目有这种奇怪的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多