Google+ android API PlusClient writeMoment is doin

2019-02-27 12:02发布

In order to implement social features in an android app, I try to use the "writeMoment" method of the "PlusClient" class, but nothing happens. I am able to get a successful connection with "PlusClient" and to write deep link posts with my app.

Here is my code when I open the Google+ connection :

monPlusClient = new PlusClient.Builder(this,
            new GooglePlayServicesClient.ConnectionCallbacks() {

                @Override
                public void onConnected() {
                    plusencours = false;
                    String accountName = monPlusClient.getAccountName();
                    // We've resolved any connection errors.
                    Toast.makeText(
                            ActiviteAfficher.this,
                            accountName
                                    + " "
                                    + ActiviteAfficher.this
                                            .getResources()
                                            .getString(
                                                    R.string.texteconnexion),
                            Toast.LENGTH_LONG).show();
                    // Log.d(TAG_DEBUG, accountName + " connected");
                }

                @Override
                public void onDisconnected() {
                    plusencours = false;
                    // Log.d(TAG_DEBUG, "disconnected");
                }

            }, new GooglePlayServicesClient.OnConnectionFailedListener() {

                @Override
                public void onConnectionFailed(ConnectionResult resultat) {
                    if (resultat.hasResolution()) {
                        try {
                            resultat.startResolutionForResult(
                                    ActiviteAfficher.this,
                                    REQUEST_CODE_RESOLVE_ERR);
                        } catch (SendIntentException e) {
                            plusencours = true;
                            monPlusClient.connect();
                        }
                    }
                    // Save the result and resolve the connection failure
                    // upon a user click.
                    mConnectionResult = resultat;
                }

            })
            .setVisibleActivities("http://schemas.google.com/AddActivity",
                    "http://schemas.google.com/DiscoverActivity")
            .setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE).build();

And here is my code when I use "writeMoment" :

ItemScope target = new ItemScope.Builder()
                .setId(monSujet.getMid())
                .setName(
                        monSujet.getName() + " - "
                                + monSujet.getNotablename())
                .setDescription(dialoguedescription).setImage(urlimage)
                .setType("http://schema.org/Person").build();
        Moment moment = new Moment.Builder()
                .setType("http://schemas.google.com/AddActivity")
                .setTarget(target).build();
        if (monPlusClient.isConnected()) {
            monPlusClient.writeMoment(moment);
        }

Understanding the logcat is difficult for me :

05-09 12:00:32.380: I/ElegantRequestDirector(27290): I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
05-09 12:00:32.380: I/ElegantRequestDirector(27290): Retrying request
05-09 12:00:33.000: E/Volley(27290): [3428] BasicNetwork.performRequest: Unexpected response code 400 for https://www.googleapis.com/plus/v1/people/me/moments/vault
05-09 12:00:33.050: D/SyncManager(295): failed sync operation XXXXXXX@gmail.com (com.google), com.google.android.gms.plus.action, USER, earliestRunTime 140603923, SyncResult: stats [ numIoExceptions: 1]
05-09 12:00:33.050: D/SyncSetupManager(16157): setState: sync = true, wantedSyncState = true
05-09 12:00:33.090: D/SyncSetupManager(16157): Enabling sync

1条回答
爷、活的狠高调
2楼-- · 2019-02-27 12:36

If you're having problems debugging issues while writing app activities, you should try enabling debug for GooglePlusPlatform:

adb shell setprop log.tag.GooglePlusPlatform VERBOSE

Which is also described here - https://developers.google.com/+/mobile/android/getting-started#frequently_asked_questions

Running your code with debugging enabled writes the following to logcat:

D/GooglePlusPlatform(8133): Unexpected response code (400) when requesting: writeMoment
D/GooglePlusPlatform(8133): Error response: {
D/GooglePlusPlatform(8133):  "error": {
D/GooglePlusPlatform(8133):   "errors": [
D/GooglePlusPlatform(8133):    {
D/GooglePlusPlatform(8133):     "domain": "global",
D/GooglePlusPlatform(8133):     "reason": "badRequest",
D/GooglePlusPlatform(8133):     "message": "Missing metadata field: http://schema.org/url."
D/GooglePlusPlatform(8133):    }
D/GooglePlusPlatform(8133):   ],
D/GooglePlusPlatform(8133):   "code": 400,
D/GooglePlusPlatform(8133):   "message": "Missing metadata field: http://schema.org/url."
D/GooglePlusPlatform(8133):  }
D/GooglePlusPlatform(8133): }

You cannot supply a Person object without supplying a public target URL which has appropriate markup (instead of an explicit name and description). Running your code with http://schema.org/Thing instead of http://schema.org/Person worked for me.

查看更多
登录 后发表回答