简体   繁体   English

Google Cloud端点自定义对象

[英]Google cloud endpoints custom objects

I was setting up endpoints using google's developer tutorials and I wanted to create my own class besides the auto-generated MyBean one, but when I try to access it, I get a 503 error. 我正在使用Google的开发人员教程设置端点,我想创建除自动生成的MyBean之外的自己的类,但是当我尝试访问它时,出现503错误。 I'm having a hard time finding information on using endpoints in android studio so any help would be appreciated. 我很难找到有关在android studio中使用端点的信息,因此将不胜感激。 Here is my code in MyEndpoints, and I just want to mention that calling the auto-generated sayHi function still works fine. 这是MyEndpoints中的代码,我只想提及调用自动生成的sayHi函数仍然可以正常工作。 Its only when I use my function. 仅当我使用函数时。

package com.example.joebruckner.fitnesstracker.backend;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;

import java.util.Date;

import javax.inject.Named;

/**
 * An endpoint class we are exposing
 */
@Api(name = "myApi", version = "v1", namespace = @ApiNamespace(
        ownerDomain = "backend.fitnesstracker.joebruckner.example.com",
        ownerName = "backend.fitnesstracker.joebruckner.example.com",
        packagePath = ""))

public class MyEndpoint {

    /**
     * A simple endpoint method that takes a name and says Hi back
     */
    @ApiMethod(name = "sayHi")
    public MyBean sayHi(@Named("name") String name) {
        MyBean response = new MyBean();
        response.setData("Hi, " + name);

        return response;
    }

    /**
     * A test to see how my own objects work
     */
    @ApiMethod(name = "getWorkout")
    public WorkoutFob getWorkout(@Named("test") String test) {
        WorkoutFob workout = new WorkoutFob(1, 2, new Date());
        return workout;
    }

}

01-02 17:28:21.939    4802-4802/joebruckner.fitnesstracker I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@620150 time:49832335
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ {
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ "code": 503,
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ "errors": [
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ {
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ "domain": "global",
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ "message": "",
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ "reason": "backendError"
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ }
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ ],
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ "message": ""
01-02 17:28:23.232    4802-4827/joebruckner.fitnesstracker W/System.err﹕ }
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at joebruckner.fitnesstracker.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:34)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at joebruckner.fitnesstracker.EndpointsAsyncTask.doInBackground(EndpointsAsyncTask.java:17)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-02 17:28:23.233    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-02 17:28:23.234    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-02 17:28:23.234    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-02 17:28:23.234    4802-4827/joebruckner.fitnesstracker W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
01-02 17:28:23.418    4802-4828/joebruckner.fitnesstracker V/RenderScript﹕ Application requested CPU execution
01-02 17:28:23.423    4802-4828/joebruckner.fitnesstracker V/RenderScript﹕ 0xa130b200 Launching thread(s), CPUs 4

package joebruckner.fitnesstracker;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.util.Pair;
import android.widget.Toast;

import com.example.joebruckner.fitnesstracker.backend.myApi.MyApi;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;

import java.io.IOException;

/**
 * Created by joebruckner on 12/31/14.
 */
public class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
    private static MyApi myApi = null;
    private final String PROJECT_CODE = "---------";
    private Context context;

    @Override
    protected String doInBackground(Pair<Context, String>... params) {
        if (myApi == null) {
            MyApi.Builder b = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                    .setRootUrl("https://" + PROJECT_CODE + ".appspot.com/_ah/api/");
            myApi = b.build();
        }

        context = params[0].first;
        String name = params[0].second;

        try {
            return myApi.getWorkout("Test 1").execute().toString();
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    }
}

Make sure you are running the backend first, by selecting the cloud backend on top if you are using Android Studio. 如果您使用的是Android Studio,请选择顶部的云后端,以确保先运行后端。 If so, we need to see your WorkoutFob class too, as well as the way you use it. 如果是这样,我们还需要查看您的WorkoutFob类以及使用它的方式。

Please update with your AsyncThread code so we can help identify the error. 请更新您的AsyncThread代码,以便我们帮助您识别错误。

EDIT: I see the problem I believe. 编辑:我看到了我相信的问题。 In your try catch block you are returning e.getMessage() instead of null like you should to the onPostExecute. 在try catch块中,您将返回e.getMessage()而不是null,就像应该返回onPostExecute一样。 Instead, return null as opposed to the e.getMessage(). 而是返回null,而不是e.getMessage()。 The Toast is most likely causing a problem since it is trying to toast a null item. 由于Toast试图烘烤一个空项目,因此Toast最有可能引起问题。 Add an if statement inside onPostExecute to test if the result is null or not from the doInBackground. 在onPostExecute内添加一个if语句,以测试doInBackground中的结果是否为null。 Provide two different Toasts for if the result is null or not, and Log.e("Error: ", e.getMessage) in the doInBackground instead. 如果结果为null,则提供两个不同的Toast,并在doInBackground中提供Log.e(“ Error:”,e.getMessage)。 Let me know if this does any good. 让我知道这是否有帮助。

Also, you could make the Context variable public and static, and create your AsyncTask as an object to add the context value, instead of passing a Pair into the AsyncTask. 另外,您可以将Context变量设为public和static,并创建AsyncTask作为添加上下文值的对象,而不是将Pair传递给AsyncTask。 Just a bit easier to read and use 易于阅读和使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM