Google云端点自定义对象

时间:2015-01-02 22:45:39

标签: java android android-studio google-cloud-endpoints

我使用google的开发人员教程设置端点,我想创建自己的类,除了自动生成的MyBean之外,但是当我尝试访问它时,我收到503错误。我很难找到有关在android studio中使用端点的信息,所以任何帮助都会受到赞赏。这是我在MyEndpoints中的代码,我只想提一下,调用自动生成的sayHi函数仍然可以正常工作。它只在我使用我的功能时。

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

1 个答案:

答案 0 :(得分:0)

如果您使用的是Android Studio,请确保首先运行后端,方法是选择云端。如果是这样,我们也需要看你的WorkoutFob类,以及你使用它的方式。

请使用您的AsyncThread代码进行更新,以便我们帮助识别错误。

编辑: 我看到了我相信的问题。在你的try catch块中,你将返回e.getMessage()而不是像onPostExecute一样返回null。相反,返回null而不是e.getMessage()。 Toast最有可能导致问题,因为它试图烘烤一个空项。在onPostExecute中添加一个if语句,以测试doInBackground中的结果是否为null。如果结果为null,则提供两个不同的Toast,而在doInBackground中提供Log.e(&#34; Error:&#34;,e.getMessage)。如果这有任何好处,请告诉我。

此外,您可以将Context变量设置为public和static,并将AsyncTask创建为添加上下文值的对象,而不是将Pair传递给AsyncTask。只是更容易阅读和使用