6

端点方法如下所示:

@Api(
    name = "gameape",
    version = "v1",
    description = "Game App API",
    audiences = { "mynumber.apps.googleusercontent.com" },
    clientIds = { "mynumber.apps.googleusercontent.com", Constant.API_EXPLORER_CLIENT_ID },
    defaultVersion = AnnotationBoolean.TRUE)
public class GameApp {

    private final AccountDao accountDao = new AccountDaoImpl();

    @ApiMethod(name = "LoginUser", path = "LoginUser", httpMethod = HttpMethod.POST)
    public void LoginUser(LoginData request) {
        long phone = request.getPhone();
        String deviceId = request.getDeviceId();
        String gcmToken = request.getGcmToken();
        Account acc = new Account(phone, deviceId, gcmToken);
        accountDao.put(acc);
        ApiHelper.sendGCM(phone, "welcome to my game app");
    }
}

来自 android 的代码片段如下所示:

@Override
protected Boolean doInBackground(Void... params) {
    LoginData request = new LoginData();
    request.setUsername(username);
    request.setPassword(password);

   try {
     RegisterUser reg = service.registerUser(request);
     reg.execute();
     return true;
   } catch (Exception e) {
     Log.e(LoginActivity.class.getName(),
        "Exception received from server at "
         + service.getRootUrl(), e);
   }
   return false;
}

调用reg.execute()不断抛出异常:

java.lang.IllegalArgumentException: the name must not be empty: null

从服务器控制台看,它甚至不像服务器被击中。即使我尝试在调试模式下运行服务器,我的断点(方法内的第一行)也没有到达。

编辑:添加堆栈跟踪:

04-03 13:38:42.688: I/com.me.gameapp.LoginActivity$UserLoginTask(11255): Enter doInBackground
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255): Exception received from server at https://1.myapp.appspot.com/_ah/api/
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255): java.lang.IllegalArgumentException: the name must not be empty: null
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at android.os.Parcel.readException(Parcel.java:1251)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at android.os.Parcel.readException(Parcel.java:1235)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.android.gms.internal.x$a$a.a(Unknown Source)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.me.gameapp.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:262)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at com.me.gameapp.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:1)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-03 13:38:42.786: E/com.me.gameapp.LoginActivity(11255):  at java.lang.Thread.run(Thread.java:1096)
04-03 13:38:42.786: I/com.me.gameapp.LoginActivity$UserLoginTask(11255): Leave doInBackground with false

在该行Exception received from server at https://1.myapp.appspot.com/_ah/api/中,我在 localhost 上运行所有内容。也许https://1.myapp.appspot.com/_ah/api/是错的。不过,我的代码非常接近模板,所以我不确定这是我提交的更改。

4

6 回答 6

12

这里引用的名称是选定的帐户名称,而不是应用程序名称。

同样在 android 6.0上,您需要在调用 google 端点之前获得联系人权限。否则,即使您使用

credential.setSelectedAccountName(accountName);

你仍然会得到上面提到的异常。

于 2016-03-17T05:30:02.057 回答
7

为自己测试时有类似的错误消息,为我解决的问题是使用

credential.setSelectedAccountName("user@gmail.com");

跑步前

SomeAbstractGoogleJsonClient.Builder builder = new SomeAbstractGoogleJsonClient.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential);

假设类似的代码在您的应用程序中。由于您只发布了调用者线程中的片段,所以很难说。

于 2013-04-15T15:26:36.003 回答
5

我的解决方案是使用

credential.setSelectedAccount(new Account(settings.getString(Constants.PREF_ACCOUNT_NAME, null), "com.example.myapplication"));

所以我用setSelectedAccount而不是setSelectedAccountName

于 2017-04-01T11:53:02.213 回答
2

设置 accountname 对我不起作用,我必须按照 marco 所说的那样设置 selectedaccount。为了简化他的回答

   credential = GoogleAccountCredential.usingOAuth2(context, scopes);
    credential.setSelectedAccount(new Account("developer@gmail.com", "com.your.pakagename"));
于 2017-08-22T09:50:28.450 回答
1

也许是一个远景,但也许它抱怨为空的“名称”是应用程序名称?

为了创建服务,我最终得到了这样的代码

HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = new JacksonFactory();

Nviewendpoint.Builder builder = new Nviewendpoint.Builder( transport, jsonFactory, null );  
builder.setApplicationName( appName );

请注意,我添加了“setApplicationName”(相对于我找到的示例)。

于 2013-04-03T21:27:52.170 回答
0

在上面添加维卡斯的答案-

从 Android 6.0(API 级别 23)开始,用户在应用运行时授予应用权限,而不是在安装应用时。

这实际上取决于您正在寻求的许可类型。对于危险权限和权限组,您需要在运行时请求权限,仅在清单中指定它是不够的。

查看危险的权限和组 - https://developer.android.com/guide/topics/security/permissions.html#perm-groups

如您所见,联系人就是其中之一-

  • READ_CONTACTS
  • WRITE_CONTACTS
  • GET_ACCOUNTS

要解决此问题,您需要在运行时请求许可。怎么做?- https://developer.android.com/training/permissions/requesting.html

于 2016-10-03T09:11:22.737 回答