【问题标题】:Google cloud endpoint on Google app engine谷歌应用引擎上的谷歌云端点
【发布时间】:2016-04-07 10:34:50
【问题描述】:

我刚刚完成了一个基于网站教程的安卓应用。这个应用程序用于向谷歌数据存储发送和接收数据。我创建了一个 appengine 后端。这在 localhost:8888 本地运行良好。我可以看到数据转换。但是在我将它部署到谷歌应用引擎之后。它无法显示数据。我可以通过 myapp.appspot.com/_ah/api/explorer 访问数据存储。但是我不能用手机访问它,而我可以用手机模拟器访问本地数据。我只是按照这位绅士的指南https://github.com/sachinkariyattin/Cloudendpoints

任何人都可以帮助我吗?提前致谢。

下面是 CloudEndpointUtils 类

package com.iot1;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.googleapis.services.AbstractGoogleClient;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;

/**
 * Common utilities for working with Cloud Endpoints.
 * 
 * If you'd like to test using a locally-running version of your App Engine
 * backend (i.e. running on the Development App Server), you need to set
 * LOCAL_ANDROID_RUN to 'true'.
 * 
 * See the documentation at
 * http://developers.google.com/eclipse/docs/cloud_endpoints for more
 * information.
 */
public class CloudEndpointUtils {

  /*
   * TODO: Need to change this to 'true' if you're running your backend locally using
   * the DevAppServer. See
   * http://developers.google.com/eclipse/docs/cloud_endpoints for more
   * information.
   */
  protected static final boolean LOCAL_ANDROID_RUN = true;

  /*
   * The root URL of where your DevAppServer is running (if you're running the
   * DevAppServer locally).
   */
  protected static final String LOCAL_APP_ENGINE_SERVER_URL = "http://localhost:8888/";

  /*
   * The root URL of where your DevAppServer is running when it's being
   * accessed via the Android emulator (if you're running the DevAppServer
   * locally). In this case, you're running behind Android's virtual router.
   * See
   * http://developer.android.com/tools/devices/emulator.html#networkaddresses
   * for more information.
   */
  protected static final String LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID = "http://10.0.2.2:8888";

  /**
   * Updates the Google client builder to connect the appropriate server based
   * on whether LOCAL_ANDROID_RUN is true or false.
   * 
   * @param builder
   *            Google client builder
   * @return same Google client builder
   */
  public static <B extends AbstractGoogleClient.Builder> B updateBuilder(
      B builder) {
    if (LOCAL_ANDROID_RUN) {
      builder.setRootUrl(LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID
          + "/_ah/api/");
    }

    // only enable GZip when connecting to remote server
    final boolean enableGZip = builder.getRootUrl().startsWith("https:");

    builder.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
    @Override
      public void initialize(AbstractGoogleClientRequest<?> request)
          throws IOException {
        if (!enableGZip) {
          request.setDisableGZipContent(true);
        }
      }
    });

    return builder;
  }

  /**
   * Logs the given message and shows an error alert dialog with it.
   * 
   * @param activity
   *            activity
   * @param tag
   *            log tag to use
   * @param message
   *            message to log and show or {@code null} for none
   */
  public static void logAndShow(Activity activity, String tag, String message) {
    Log.e(tag, message);
    showError(activity, message);
  }

  /**
   * Logs the given throwable and shows an error alert dialog with its
   * message.
   * 
   * @param activity
   *            activity
   * @param tag
   *            log tag to use
   * @param t
   *            throwable to log and show
   */
  public static void logAndShow(Activity activity, String tag, Throwable t) {
    Log.e(tag, "Error", t);
    String message = t.getMessage();
    // Exceptions that occur in your Cloud Endpoint implementation classes
    // are wrapped as GoogleJsonResponseExceptions
    if (t instanceof GoogleJsonResponseException) {
      GoogleJsonError details = ((GoogleJsonResponseException) t)
          .getDetails();
      if (details != null) {
        message = details.getMessage();
      }
    }
    showError(activity, message);
  }

  /**
   * Shows an error alert dialog with the given message.
   * 
   * @param activity
   *            activity
   * @param message
   *            message to show or {@code null} for none
   */
  public static void showError(final Activity activity, String message) {
    final String errorMessage = message == null ? "Error" : "[Error ] "
        + message;
    activity.runOnUiThread(new Runnable() {
    @Override   
      public void run() {
        Toast.makeText(activity, errorMessage, Toast.LENGTH_LONG)
            .show();
      }
    });
  }
}

【问题讨论】:

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


    【解决方案1】:

    我猜您没有在 Google Cloud 创建自己的帐户和/或在 appengine-web.xml 文件中进行必要的更改。对于一个完整的示例,您可以遵循:http://rominirani.com/2014/08/20/gradle-tutorial-part-7-android-studio-app-engine-gradle/

    【讨论】:

    • 谢谢。我有在 Google Cloud 上创建的项目。我确实更改了 appengine-web.xml。当我使用 myapp.appspot.com/_ah/api/explorer 时它可以工作唯一的错误是我无法通过电话访问 Google Cloud 数据存储区。
    • 您是否更改了端点 API 中的 client_id(WEB_CLIENT_ID 和 ANDROID_CLIENT_ID)?
    • 如何更改WEB_CLIENT_ID和ANDROID_CLIENT_ID?
    • 它们被定义在端点类的开头,就在类声明之前,在@Api(.......)中
    • 我正在使用 Eclipse。它只有一个 CloudEndpointUtils 类。它没有任何端点的类。我应该把@Api 放在这里。下面是 CloudEndpointUtils.java:
    【解决方案2】:

    我发现我应该在更改 appengine-web.xml 后更新创建的谷歌客户端库。现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 2016-11-28
      相关资源
      最近更新 更多