6

The endpoint method looks like this:

@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");
    }
}

The snippet from android looks like this:

@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;
}

Calling reg.execute() keeps throwing the exception:

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

From the server console, it does not even look like the server is being hit. Even when I try running the server in debug mode, my breakpoint (first line inside method) is not reached.

EDIT: adding stack trace:

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

In the line Exception received from server at https://1.myapp.appspot.com/_ah/api/, I am running everything on localhost. Maybe https://1.myapp.appspot.com/_ah/api/ is wrong. My code is very close to the template, though, so I am not sure that's a change I committed.

learner
  • 11,490
  • 26
  • 97
  • 169
  • Can you share the full stack trace? I suspect the error is in your Android code. – Dan Holevoet Apr 03 '13 at 20:29
  • @DanHolevoet I have added the stack trace. – learner Apr 03 '13 at 20:46
  • Also, I am able to hit the server thru the api explorer; but not thru the android code. – learner Apr 03 '13 at 21:51
  • 1
    I'd suggest trying to change `DEFAULT_ROOT_URL` in the generated code to `http://localhost:your_port/_ah/api/`. If it works in the Explorer, it suggests the backend itself is running just fine. – Dan Holevoet Apr 03 '13 at 22:30
  • @DanHolevoet I actually did that. But the error didn't change. I have been trying all sorts of thing to see if I can get a hit. The problem seems to be in the `credential` bit. If I replace it with null, the call times out, but I don't get the error. Also I get the error whether the server is running or not. – learner Apr 03 '13 at 22:49
  • I think your API invocation code is also incorrect. The usual convention is `service.resource().method().execute()`. You just have `service().method().execute()`. Check https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-android/blob/master/src/com/google/devrel/samples/ttt/TictactoeActivity.java#L255 for an example API invocation. If you're using Eclipse, the code completion feature should help you find the right call to make to `service`. – Dan Holevoet Apr 03 '13 at 22:57
  • @DanHolevoet but my endpoint is returning `void`. – learner Apr 03 '13 at 22:59
  • What your Endpoint returns is unrelated to the method in which you call the API. Can you post the rest of your Endpoint class? – Dan Holevoet Apr 03 '13 at 23:05
  • @DanHolevoet I have updated to show the api. Yes, I am using the eclipse plugin. And I am looking through the code completion. I am not seeing any resource I can attach to service. – learner Apr 03 '13 at 23:30
  • @DanHolevoet So I changed `name = "LoginUser"` to `name = "hello.LoginUser"` and removed the `path` piece so it can look just like the tictacto code. Then I fix the android to be `hello().loginUser(request)`. Then I still get the same error. – learner Apr 03 '13 at 23:50

6 Answers6

12

Here the name being referred is the selected account name, not the application name.

Also on android 6.0 you need to get the Contacts permission before you invoke google endpoints. Otherwise even if you set the account name using

credential.setSelectedAccountName(accountName);

You will still get the exception mentioned above.

Vikas
  • 4,263
  • 1
  • 34
  • 39
  • Thanks, this information 'saved' my life after a lot of time looking for the stupid Contacts Permission!!!!!! thank you – user2582318 Aug 18 '16 at 02:10
7

Had a similar error message when testing this out for myself, what solved it for me was using

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

before running

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

assuming similar code is somewhere in your app. As you have only posted the snippet from your caller thread it's kinda hard to tell.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
user2072160
  • 254
  • 2
  • 12
5

My solution was to use

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

So I used setSelectedAccount instead of setSelectedAccountName

marcolav
  • 405
  • 1
  • 6
  • 17
2

Setting accountname did not work for me , i had to set selectedaccount as marco said . To simplify his answer

   credential = GoogleAccountCredential.usingOAuth2(context, scopes);
    credential.setSelectedAccount(new Account("developer@gmail.com", "com.your.pakagename"));
Manohar
  • 22,116
  • 9
  • 108
  • 144
1

A longshot perhaps, but maybe the 'name' that it is complaining about being null is the application name?

For creating the service I ended up with code like this

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

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

Note that I added the 'setApplicationName' (relative to the examples I found).

Tom
  • 17,103
  • 8
  • 67
  • 75
0

Adding to Vikas's answer above -

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

It actually depends on the type of permission you are seeking. For Dangerous permissions and permission groups you need to ask for permission at runtime and just specifying it in manifest is not enough.

To see dangerous permissions and groups - https://developer.android.com/guide/topics/security/permissions.html#perm-groups

As you can see CONTACTS is one of them -

  • READ_CONTACTS
  • WRITE_CONTACTS
  • GET_ACCOUNTS

To fix this you need to ask for permission at runtime. How to do that? - https://developer.android.com/training/permissions/requesting.html

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289