1

我们可以通过 Android 的 REST API 访问谷歌调查表并通过 Android 在谷歌调查中发布填充数据吗?

4

1 回答 1

0

这里有一个java库,可以这样实现:

假设您已经在开发者控制台上设置了调查 API。

您将库绑定到您的项目:

implementation 'com.google.apis:google-api-services-surveys:v2-rev16-1.25.0'.

如果您Gson用作 json 解析器,则需要 exclude Jackson,因为它带有调查 java 库。那么你的 gradle 条目看起来像这样:


implementation("com.google.apis:google-api-services-surveys:v2-rev16-1.25.0") {
        exclude module: 'google-http-client-jackson2'
        exclude module: 'commons-logging'
        exclude module: 'httpcore'
        exclude module: 'httpclient' // excluding apache dependencies, too, since we don't need them.
    }

为了Gson与调查 API 一起使用,您绑定了 gson google api 客户端:

implementation "com.google.api-client:google-api-client-gson:1.28.0"

有了它,您可以通过以下方式执行调查:

...

private void sendSurvey() {
    Survey mySurvey = new Survey().set("Hello", "World");
    new SendSurveyAsyncTask().execute(mySurvey);
}

...
private static class SendSurveyAsyncTask extends AsyncTask<Survey, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Survey... surveys) {
            try {
                // using the NetHttpTransport here. You could even write your own retrofit transport for that, if you want.
                // See https://developers.google.com/api-client-library/java/google-http-java-client/android
                new Surveys.Builder(new NetHttpTransport(), new GsonFactory(), null)
                        .build()
                        .surveys()
                        .insert(surveys[0])
                        .execute();
            } catch (IOException e) {
                Log.e("SendSurveyAsyncTask", "oops", e);
                return false;
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                Log.d("SendSurveyAsyncTask", "whoo!");
            } else {
                Log.d("SendSurveyAsyncTask", "boo!");
            }
        }
    }

希望能帮助到你 :)

干杯

于 2019-02-10T14:21:54.183 回答