java - 无法在 Google App Engine 端点中使用插入方法

标签 java android google-app-engine android-asynctask google-cloud-endpoints

我正在构建一个访问服务器的 Android 应用程序。我正在使用完整的谷歌解决方案。后端位于 GAE 中,我使用端点来公开我的 API,我还使用 GCM。我使用 android studio 提供的自动生成工具来获取我的类(class)。

在我的应用程序模块中,我有一个名为 Offer 的类,这是我将要发送到服务器的数据放入的位置,我还有一个允许进行 api 调用的 AsyncTask 类。

在我的后端模块中,我有公开的 API,而且我还有一个类提供,API 由 android studio 和应用程序引擎 sdk 生成。

现在我的问题是我尝试了,但结果失败了,就像应用程序和后端中的类不兼容一样。虽然它们是相同的,但实际上后端的只是应用程序中的简单复制,区别在于我添加的“objectify”注释。以下是我的代码 fragment 和项目结构的屏幕截图。

    public class InsertOfferAsyncTask extends AsyncTask <Offer, Void, Boolean>{

    private static OfferApi offer_service;
    private Context context;

    public InsertOfferAsyncTask(Context context) {
     this.context = context;
    }

    protected Boolean doInBackground(Offer... offer) {

      if (offer_service == null) {

        OfferApi.Builder builder = new OfferApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
                .setRootUrl("https://flawless-snow-95011.appspot.com/_ah/api/");


        offer_service = builder.build();
    }


    try {
        offer_service.insert(offer[0]); //this where I make the actual API call, I know I shouldn't use Object, it was an attempt to make it work
    } catch (IOException e) {
        e.printStackTrace();
    }


    return true;
    }

这是我调用 AsyncTask 的部分,即上面的代码。

     Log.i("offer", offer.getUsr_id());
            Log.i("offer_id", String.valueOf(offer.getId()));
            Log.i("offer_date", offer.getPost());
            new         InsertOfferAsyncTask(getActivity().getBaseContext()).execute(offer);
            getActivity().finish();

上面的所有代码都取 self 的应用程序模块,以下是代码生成的端点代码,我仅发布我调用的部分。

    @ApiMethod(
        name = "insert",
        path = "offer",
        httpMethod = ApiMethod.HttpMethod.POST)
public Offer insert(Offer offer) {

    ofy().save().entity(offer).now();
    logger.info("Created Offer with ID: " + offer.getId());

    return ofy().load().entity(offer).now();
}

Screenshot of my project tree

我现在需要的是如何使用我所拥有的将数据发送到服务器。我知道我可以连接到服务器,我测试过。

这是我尝试构建时收到的错误消息。

    Error:(233, 73) error: no suitable method found for execute(.model.Offer) 
    method AsyncTask.execute(Runnable) is not applicable
    (actual argument .model.Offer cannot be converted to Runnable by method invocation conversion)
    method AsyncTask.execute(backend.model.offerApi.model.Offer...) is not applicable
    (argument type app.model.Offer does not conform to vararg element type backend.model.offerApi.model.Offer)

有什么帮助吗?我应该使用 JSON (我怀疑,这项工作是由自动生成的类完成的,如构建器中所示)

最佳答案

您是否正在使用两个不同的“Offer”对象 app.model.Offerbackend.model.offerApi.model.Offer

类型 backend.model.offerApi.model.Offer 似乎是为您的 Endpoints API 生成的类型,您需要在客户端 (android) 端的任何地方使用该类型。

关于java - 无法在 Google App Engine 端点中使用插入方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30335759/

相关文章:

java - 数据表警告 : table id=example - Ajax error

c# - 多重继承

android - 用手指移动任何 RelativeLayout(不改变布局参数)

python - GAE 查询中如何实现 filter() 和 get()?

iphone - 使用谷歌账户登录

java - 不通过 while (rs.next()) 处理所需的输出

java - 如何将重复调用的外部进程集成到Java webapp中?

android - 在android中使用粘性标题在视差ListView上启用点击事件

java - 数据未在代码名称 BouncyCaSTLe 中对齐 block 大小(无填充)

google-app-engine - GAE 新创建的任务队列是 "disabled"但工作正常吗?