我们可以通过 REST API for Android 访问 google 调查表单,并通过 Android 在 google 调查中填写 POST 数据吗?
这里有一个Java库,它可以像这样实现:
假设您已经在开发人员控制台上设置了调查 API。
将库绑定到项目:
implementation 'com.google.apis:google-api-services-surveys:v2-rev16-1.25.0'
.
如果您使用Gson
作为 json 解析器,则需要排除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 谷歌 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!");
}
}
}
希望对:)有所帮助
干杯