Google应用引擎上的Google云端点

时间:2022-05-16 20:20:30

I just complete a android app based on a website tutorial. This app is to send and receive data to google datastore. I have created a appengine backend. This works well locally on localhost:8888. I could see the data transformation. But after I deploy it to google app engine. It can not show the data. I could access the datastore by myapp.appspot.com/_ah/api/explorer. But I can not access it with phone while I can access local data with phone emulator. I just followed this gentleman's guide https://github.com/sachinkariyattin/Cloudendpoints

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

Any one can help me? Thanks in advance.

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

The Below is the CloudEndpointUtils class

以下是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();
      }
    });
  }
}

2 个解决方案

#1


0  

I guess you did not create your own account at Google Cloud and/or make the necessary changes in appengine-web.xml file. For a thorough example you may follow: http://rominirani.com/2014/08/20/gradle-tutorial-part-7-android-studio-app-engine-gradle/

我猜您没有在Google Cloud上创建自己的帐户和/或在appengine-web.xml文件中进行必要的更改。有关详细示例,请访问:http://rominirani.com/2014/08/20/gradle-tutorial-part-7-android-studio-app-engine-gradle/

#2


0  

I found it that I should update the created google client library after I changed the appengine-web.xml. And it all works now.

我发现在更改appengine-web.xml后我应该更新创建的google客户端库。这一切现在都有效。

#1


0  

I guess you did not create your own account at Google Cloud and/or make the necessary changes in appengine-web.xml file. For a thorough example you may follow: http://rominirani.com/2014/08/20/gradle-tutorial-part-7-android-studio-app-engine-gradle/

我猜您没有在Google Cloud上创建自己的帐户和/或在appengine-web.xml文件中进行必要的更改。有关详细示例,请访问:http://rominirani.com/2014/08/20/gradle-tutorial-part-7-android-studio-app-engine-gradle/

#2


0  

I found it that I should update the created google client library after I changed the appengine-web.xml. And it all works now.

我发现在更改appengine-web.xml后我应该更新创建的google客户端库。这一切现在都有效。