1

Android から呼び出す次のリンクを使用してプロジェクトをセットアップし、認証バックエンドを追加しました。資格情報と SHA1 キーを再確認しました。

しかし、私はまだ次のエラーが発生しています

    java.io.IOException: NetworkError
    at com.google.android.gms.auth.zzd$1.zzbr(Unknown Source)
    at com.google.android.gms.auth.zzd$1.zzbs(Unknown Source)
    at com.google.android.gms.auth.zzd.zza(Unknown Source)
    at com.google.android.gms.auth.zzd.zzc(Unknown Source)
    at com.google.android.gms.auth.zzd.getToken(Unknown Source)
    at com.google.android.gms.auth.zzd.getToken(Unknown Source)
    at com.google.android.gms.auth.zzd.getToken(Unknown Source)
    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at com.shantanu.hellocloudendpoints.EndPointAsyncTask.doInBackground(EndPointAsyncTask.java:53)
    at com.shantanu.hellocloudendpoints.EndPointAsyncTask.doInBackground(EndPointAsyncTask.java:22)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)

これは以下の私のコードです:

    public class MainActivity extends AppCompatActivity {
        private static final int REQUEST_ACCOUNT_PICKER = 2;
        private SharedPreferences settings;
        private String accountName;
        private GoogleAccountCredential credential;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            settings = getSharedPreferences("FamousQuotesAndroid1", 0);
            credential = GoogleAccountCredential.usingAudience(this, "server:client_id:xxxxxxxxx.apps.googleusercontent.com");
            setAccountName(settings.getString("ACCOUNT_NAME", null));
            if (credential.getSelectedAccountName() == null) {
                chooseAccount();
            }
        }

        private void setAccountName(String accountName) {
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("ACCOUNT_NAME", accountName);
            editor.commit();
            credential.setSelectedAccountName(accountName);
            this.accountName = accountName;
        }

        void chooseAccount() {
            startActivityForResult(credential.newChooseAccountIntent(),
                    REQUEST_ACCOUNT_PICKER);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode,
                                        Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
                case REQUEST_ACCOUNT_PICKER:
                    if (data != null && data.getExtras() != null) {
                        String accountName =
                                data.getExtras().getString(
                                        AccountManager.KEY_ACCOUNT_NAME);
                        if (accountName != null) {
                            setAccountName(accountName);
                        }
                    }
                    break;

            }
        }

        public void getQuotes(View v) {
            new EndPointAsyncTask(this).execute();
        }

        public GoogleAccountCredential getCredential() {
            return credential;
        }
    }





 @Override
    protected List<Quote> doInBackground(Void... params) {
        if (myApiService == null) { // Only do this once
            QuoteApi.Builder builder = new
                    QuoteApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), context.getCredential()).setRootUrl("https://xxxxx.appspot.com/_ah/api/");
            builder.setApplicationName(context.getPackageName());
            myApiService = builder.build();
        }
        try {
            return myApiService.listQuote().execute().getItems();
        } catch (IOException e) {
            e.printStackTrace();
            return Collections.EMPTY_LIST;
        }
    }

エンドポイント法

@ApiMethod(name = "listQuote", scopes = {Constants.EMAIL_SCOPE},
            clientIds = {Constants.WEB_CLIENT_ID,
                    Constants.ANDROID_CLIENT_ID,
                    com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
            audiences = {Constants.ANDROID_AUDIENCE})
    public CollectionResponse<Quote> listQuote(@Nullable @Named("cursor") String cursorString,
                                               @Nullable @Named("count") Integer count, User user) throws UnauthorizedException {
        if (user == null) throw new UnauthorizedException("User is Not Valid");
        Query<Quote> query = ofy().load().type(Quote.class);
        if (count != null) query.limit(count);
        if (cursorString != null && cursorString != "") {
            query = query.startAt(Cursor.fromWebSafeString(cursorString));
        }
        List<Quote> records = new ArrayList<Quote>();
        QueryResultIterator<Quote> iterator = query.iterator();
        int num = 0;
        while (iterator.hasNext()) {
            records.add(iterator.next());
            if (count != null) {
                num++;
                if (num == count) break;
            }
        }
//Find the next cursor
        if (cursorString != null && cursorString != "") {
            Cursor cursor = iterator.getCursor();
            if (cursor != null) {
                cursorString = cursor.toWebSafeString();
            }
        }
        return CollectionResponse.<Quote>builder().setItems(records).setNextPageToken(cursorString).build();
    }
4

1 に答える 1

1

最後に問題を見つけました。テストに使用していたデバイスでは、Google Play サービスのバックグラウンド データが制限されていました。バックグラウンド データの制限が削除されると、機能し始めました。

于 2016-09-16T13:22:51.527 に答える