Google Cloud Endpoint错误:包MyApi不存在

时间:2018-08-30 18:39:26

标签: android google-cloud-endpoints-v2

运行MainActivity时收到此error: package MyApi does not exist消息。 StackOverFlow中也有类似的帖子,但是该解决方案对我不起作用。

没有logcat堆栈跟踪。发生的情况是myApi,即Google Cloud Module后端的一个类中的API的名称,未被识别,我真的不知道为什么。

Api所在的类(模块后端)

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 com.joketellingapp.lacourt.jokesource.JokeSource;

import javax.inject.Named;

/** An endpoint class we are exposing */
@Api(
        name = "myApi",
        version = "v1",
        namespace = @ApiNamespace(
                ownerDomain = "backend.builditbigger.gradle.udacity.com",
                ownerName = "backend.builditbigger.gradle.udacity.com",
                packagePath = ""
        )
)
public class MyEndpoint {

    @ApiMethod(name = "sayHi")
    public MyBean sayHi() {
        MyBean response = new MyBean();
        response.setData(new JokeSource().getJoke());

        return response;
    }

}

我要从中访问API(模块应用)的类

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

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.joketellingapp.lacourt.displayjoke.DisplayJoke;
import com.udacity.gradle.builditbigger.backend.myApi.MyApi;

import java.io.IOException;

class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
    private static MyApi myApiService = null;
    private Context context;

    @Override
    protected String doInBackground(Pair<Context, String>... params) {
        if(myApiService == null) {  // Only do this once
            MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                // options for running against local devappserver
                // - 10.0.2.2 is localhost's IP address in Android emulator
                // - turn off compression when running against local devappserver
                .setRootUrl("http://10.0.2.2:8888/_ah/api/")
                .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                    @Override
                    public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                        abstractGoogleClientRequest.setDisableGZipContent(true);
                    }
                });
                // end options for devappserver

            myApiService = builder.build();
        }

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

        try {
            return myApiService.sayHi().execute().getData();
        } catch (IOException e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
//        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(context, DisplayJoke.class);
        intent.putExtra(DisplayJoke.JOKE_KEY, result);
        context.startActivity(intent);

    }


}

Build.gradle文件(模块应用程序)

apply plugin: 'com.android.application'
apply plugin: 'com.google.cloud.tools.endpoints-framework-client'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.google.cloud.tools:endpoints-framework-gradle-plugin:1.0.2'
    }
}

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.joketellingapp.lacourt.jokeapp"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

//    implementation project(path: ':backend', configuration: 'endpoints')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.google.android.gms:play-services-ads:11.8.0'

    implementation 'com.google.api-client:google-api-client:1.23.0'
    implementation 'com.google.http-client:google-http-client-android:1.23.0'
    implementation 'com.google.code.findbugs:jsr305:3.0.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    api project(':jokeSource')
    api project(':displayjoke')
    api project(path: ':backend', configuration: 'endpoints')
//    api project(path: ':backend')
}

Build.gradle文件(模块后端)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.google.cloud.tools:endpoints-framework-gradle-plugin:1.0.2'
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:1.3.3'
    }
}

repositories {
    jcenter()
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'com.google.cloud.tools.appengine'
apply plugin: 'com.google.cloud.tools.endpoints-framework-server'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

appengine.run.port = 8888

dependencies {
    implementation 'com.google.endpoints:endpoints-framework:2.0.9'
    implementation 'javax.inject:javax.inject:1'

    implementation 'javax.servlet:servlet-api:2.5'
    implementation 'com.google.api-client:google-api-client:1.23.0'
    implementation 'com.google.http-client:google-http-client-android:1.23.0'

    compile project(path: ':jokeSource')
}

顶级Build.gradle文件

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath 'com.google.guava:guava:22.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir;
}

1 个答案:

答案 0 :(得分:0)

根据the new plugin docs,它应该是endpointsServer project(path: ":server", configuration: "endpoints")