簡體   English   中英

Google App Engine上的Google Cloud端點

[英]Google cloud endpoint on Google app engine

我剛剛完成了一個基於網站教程的android應用程序。 這個程序是用來發送和接收數據到谷歌數據存儲。 我創建了一個appengine后端。 這在本地主機上運行良好:8888。 我可以看到數據轉換。 但是在我將其部署到Google App Engine之后。 它無法顯示數據。 我可以通過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();
      }
    });
  }
}

我猜您沒有在Google Cloud上創建自己的帳戶和/或沒有在appengine-web.xml文件中進行必要的更改。 有關完整的示例,您可以遵循: http : //rominirani.com/2014/08/20/gradle-tutorial-part-7-android-studio-app-engine-gradle/

我發現更改appengine-web.xml后應該更新創建的google客戶端庫。 現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM